<?php

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

class AIEO_Third_Party_Blocks_Bridge {

    public static function bootstrap() {
        add_action('wp_enqueue_scripts', array(__CLASS__, 'force_enqueue_box_now_blocks'), 12);
        add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_address_autocomplete_blocks'), 12);
        
        
        
        
        
        add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_box_now_throttle'), 5);
    }

    
    public static function enqueue_box_now_throttle() {
        if (!function_exists('is_checkout') || (!is_checkout() && !is_cart())) { return; }
        $rel = 'assets/js/aieo-box-now-throttle.js';
        $abs = AIEO_DIR . $rel;
        if (!file_exists($abs)) { return; }
        wp_enqueue_script(
            'aieo-box-now-throttle',
            AIEO_URI . $rel,
            array(),
            (string) filemtime($abs),
            false 
        );
    }

    
    public static function enqueue_address_autocomplete_blocks() {
        return;
    }

    public static function force_enqueue_box_now_blocks() {
        if (!function_exists('is_checkout') || !is_checkout()) { return; }
        if (wp_script_is('box-now-delivery-blocks-js', 'enqueued')) { return; }

        
        
        
        $plugin_dir = WP_PLUGIN_DIR . '/box-now-delivery';
        $blocks_js  = $plugin_dir . '/js/box-now-delivery-blocks.js';
        if (!file_exists($blocks_js)) { return; }

        
        
        
        
        
        if (!wp_script_is('box-now-delivery-js', 'enqueued')) { return; }

        wp_enqueue_script(
            'box-now-delivery-blocks-js',
            plugins_url('js/box-now-delivery-blocks.js', $plugin_dir . '/box-now-delivery.php'),
            array('wp-data'),
            defined('BOX_NOW_DELIVERY_VERSION') ? BOX_NOW_DELIVERY_VERSION : (string) filemtime($blocks_js),
            true
        );

        
        
        
        
        
        $settings = self::box_now_settings();
        if (!empty($settings)) {
            wp_localize_script('box-now-delivery-blocks-js', 'boxNowDeliverySettings', $settings);
        }
    }

    private static function box_now_settings() {
        if (!function_exists('get_option')) { return array(); }
        $cod_description = '';
        if (function_exists('bndp_get_custom_cod_description_for_current_zone')) {
            $cod_description = bndp_get_custom_cod_description_for_current_zone();
        }
        return array(
            'partnerId'                => esc_attr(get_option('boxnow_partner_id', '')),
            'embeddedIframe'           => esc_attr(get_option('embedded_iframe', '')),
            'displayMode'              => esc_attr(get_option('box_now_display_mode', 'popup')),
            'buttonColor'              => esc_attr(get_option('boxnow_button_color', '#6CD04E ')),
            'buttonText'               => esc_attr(get_option('boxnow_button_text', 'Pick a Locker')),
            'lockerNotSelectedMessage' => esc_js((string) get_option('boxnow_locker_not_selected_message', 'Please select a locker first!')),
            'gps_option'               => get_option('boxnow_gps_tracking', 'on'),
            'codTitle'                 => __('BOX NOW PAY ON THE GO!', 'ai-eshop-optimizer'),
            'codDescription'           => $cod_description,
            'ajaxUrl'                  => admin_url('admin-ajax.php'),
            'nonce'                    => wp_create_nonce('box-now-delivery-nonce'),
            'page'                     => 'checkout',
        );
    }
}
