<?php

namespace AdvancedBlockControls;

// Exit if the file is accessed directly
if ( !defined('ABSPATH') ) {
    exit; 
}

class AssetsManager
{

    public function __construct()
    {
        add_action( 'enqueue_block_editor_assets', [$this, 'enqueue_editor_scripts'], 10);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts'], 10);
    }

    public function enqueue_editor_scripts()
    {
        $restApiController = new RestApiController();
        $all_settings = $restApiController->get_all_settings();

        // show controls hook
        $show_controls = apply_filters('abcon/editor/show_controls', true);

        // Define hooks as an array and add $show_controls as a key
        $hooksData = array(
            'show_controls' => $show_controls
        );

        // Register the script first
        wp_register_script(
            'advanced_block_controls_js',
            plugin_dir_url(dirname(__FILE__))  . '/js/editor-scripts/index.js',
            array('wp-element'), // Dependencies
            ABCON_VERSION,
            true // Load script in footer
        );

        // Localize the data to be available in the script
        wp_localize_script(
            'advanced_block_controls_js',
            'AdvancedBlockControlsSettings',
            array(
                "settings" => $all_settings,
                "hooksData" => $hooksData,
                "attributeName" => AttributesManager::ATTRIBUTE_KEY
            )
        );

        // Enqueue the script
        wp_enqueue_script('advanced_block_controls_js');
    }

    public function enqueue_admin_scripts($hook_suffix)
    {
        // Check if this is our settings page, whether it's top-level or under a parent menu
        // Top-level: 'toplevel_page_advanced-block-controls-settings'
        // Under parent: '{parent}_page_advanced-block-controls-settings' (e.g., 'themes_page_advanced-block-controls-settings')
        $is_settings_page = ($hook_suffix === 'toplevel_page_' . MenuManager::$menu_slug) 
            || (strpos($hook_suffix, '_page_' . MenuManager::$menu_slug) !== false);
        
        if ($is_settings_page) {
            $block_registry = \WP_Block_Type_Registry::get_instance();

            // Register Block Categories.
            $block_categories = [];
            if ( function_exists( 'get_block_categories' ) ) {
                $block_categories = get_block_categories( get_post() );
            }

            wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $block_categories ) ), 'after' );

            foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
                // editor script.
                if ( ! empty( $block_type->editor_script ) ) {
                    wp_enqueue_script( $block_type->editor_script );
                }

                // Enqueue style also
                if ( ! empty( $block_type->editor_style ) ) {
                    wp_enqueue_style( $block_type->editor_style );
                }
            }

            // -> https://github.com/WordPress/gutenberg/issues/22812
            wp_add_inline_script( 'wp-blocks', 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' );

            // Settings Page 
        
                wp_enqueue_script(
                    'advanced_block_controls_settings_js',
                    plugin_dir_url(dirname(__FILE__))  . '/js/settings-scripts/index.js',
                    ['wp-blocks', 'wp-editor', 'wp-element', 'wp-components', 'wp-i18n', 'wp-edit-post', "wp-block-library"],
                    ABCON_VERSION,
                    true // Load script in footer 
                );

                wp_localize_script('advanced_block_controls_settings_js', 'wpApiSettings', array(
                    'nonce' => wp_create_nonce('wp_rest')
                ));
            

            // Enqueue editor scripts
            do_action( 'enqueue_block_editor_assets' );
        }
    }
}
