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

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

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id        = sanitize_html_class( $node['id'] ?? '' );
        $content   = $node['content'] ?? [];
        $styles    = $node['styles']  ?? [];
        $shortcode = trim( $content['shortcode'] ?? '' );
        $a         = $renderer->get_css_attrs( $node );

        if ( ! $shortcode ) {
            return '';
        }

        $renderer->add_css( '.jalpi-el.jalpi-' . $id . '{display:block;' . $renderer->styles_to_css( $styles ) . '}' );

        // do_shortcode() output is trusted — each shortcode handler is responsible for its own escaping per WordPress convention.
        // Wrapped with wp_kses_post() as an additional safety layer.
        return '<div class="jalpi-el jalpi-shortcode jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>' . wp_kses_post( do_shortcode( $shortcode ) ) . '</div>';
    }
}
