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

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

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id      = sanitize_html_class( $node['id'] ?? '' );
        $content = $node['content'] ?? [];
        $styles  = $node['styles']  ?? [];
        // Content is already sanitized at save time (Jalpi_Sanitizer via
        // kses_html_block). Re-escape here with the builder's own allowed-HTML
        // set — which permits inline SVG/data-* — rather than wp_kses_post(),
        // which would strip SVG icons embedded in custom HTML blocks.
        $html    = wp_kses( $content['html'] ?? '', Jalpi_Renderer::allowed_html() );
        $a       = $renderer->get_css_attrs( $node );

        if ( '' === $html ) {
            return '';
        }

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

        return '<div class="jalpi-el jalpi-html jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>' . $html . '</div>';
    }
}
