<?php
defined( 'ABSPATH' ) || exit;

class Jalpi_Element_Section extends Jalpi_Element {
    public static function type(): string { return 'section'; }

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id       = sanitize_html_class( $node['id'] ?? '' );
        $settings = $node['settings'] ?? [];
        $styles   = $node['styles']   ?? [];
        $css      = $renderer->section_css( $id, $settings, $styles );

        if ( $css ) {
            $renderer->add_css( $css );
        }

        // CSS ID / CSS Class from settings (Settings tab) — also resolves any
        // attached reusable classes (Class-Based Styling System).
        $attrs = $renderer->get_css_attrs( $node );

        // Full-width sections break out of the WordPress theme content container.
        $cw       = sanitize_text_field( $settings['contentWidth'] ?? 'full' );
        $fw_class = ( $cw === 'full' ) ? ' jalpi-section--fw' : '';

        $class    = 'jalpi-section' . ( $id ? ' jalpi-' . $id : '' ) . $fw_class . $attrs['extra_cls'];
        $id_attr  = $attrs['id_attr'];

        // Overlay div (shown when overlayNormal is configured)
        $ov         = $settings['overlayNormal'] ?? [];
        $overlay_html = ( ! empty( $ov['color'] ) || ! empty( $ov['imageUrl'] ) || ! empty( $ov['gradient'] ) )
            ? '<div class="jalpi-section-overlay" aria-hidden="true"></div>'
            : '';

        return '<section class="' . $class . '"' . $id_attr . '>'
             . $overlay_html
             . $renderer->render_children( $node )
             . '</section>';
    }
}
