<?php
/**
 * MenuX Free — Footer Gutenberg Block (giuliomax/footer)
 *
 * Dynamic block that reuses the shortcode renderer (menux_render_footer_shortcode),
 * mirroring the pattern of the giuliomax/menu block.
 *
 * @package MenuX
 */

if ( ! defined( 'ABSPATH' ) ) exit;

add_action( 'init', 'menux_register_footer_block' );

function menux_register_footer_block() {
    register_block_type(
        MENUX_DIR . 'blocks/menux-footer',
        [ 'render_callback' => 'menux_footer_block_render_callback' ]
    );
}

function menux_footer_block_render_callback( $attributes ) {
    $html = menux_render_footer_shortcode( array() );

    if ( empty( $html ) ) {
        // Editor-only hint so the block is visible/selectable before configuration.
        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
            return '<div style="padding:24px;text-align:center;background:#f1f5f9;border:1px dashed #cbd5e1;border-radius:8px;color:#64748b;font-size:13px;">'
                 . esc_html__( 'MenuX Footer is not configured yet. Build it under Menu Builder → Footer.', 'giuliomax-menu-builder' )
                 . '</div>';
        }
        return '';
    }

    /*
     * ServerSideRender preview runs in a sandboxed iframe without the normally
     * enqueued styles, so inline Font Awesome + generated footer CSS (same
     * approach as the menu block).
     */
    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
        $css     = menux_generate_footer_css( get_option( 'menux_footer_style', array() ) );
        $fa6_url = MENUX_URL . 'assets/fa6/css/all.min.css';
        $prefix  = '<link rel="stylesheet" href="' . esc_url( $fa6_url ) . '">';
        $prefix .= '<style>' . wp_strip_all_tags( $css ) . '</style>';
        return $prefix . $html;
    }

    return $html;
}
