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

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

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

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

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

        $src = 'https://maps.google.com/maps?q=' . rawurlencode( $address ) . '&output=embed';

        return '<div class="jalpi-el jalpi-maps jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>'
             . '<iframe src="' . esc_url( $src ) . '" width="100%" height="' . esc_attr( $height ) . '" style="border:0;display:block;" allowfullscreen loading="lazy" title="Map"></iframe>'
             . '</div>';
    }
}
