<?php

/**
 * Eryndo Block Assets
 *
 * This file handles the registration and enqueuing of theme assets,
 * including styles and scripts for both the frontend and editor.
 *
 * @package Eryndo
 * @since 1.0.0
 */

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

if (! class_exists('Eryndo_Block_Assets')) {
    /**
     * Eryndo_Block_Assets Class
     * 
     * Manages the loading of theme assets for blocks and editor.
     */
    class Eryndo_Block_Assets {

        /**
         * Constructor
         * 
         * Sets up the hook for enqueuing block assets.
         */
        public function __construct() {
            add_action('enqueue_block_assets', [$this, 'enqueue_block_assets']);
        }

        /**
         * Enqueue block assets
         * 
         * Registers and enqueues styles and scripts for blocks.
         * Handles both frontend and editor assets.
         */
        public function enqueue_block_assets() {
            // Get theme version for cache busting
            $theme_version = wp_get_theme()->get('Version');

            // Register and enqueue styles
            wp_register_style(
                'eryndo-block-styles',
                get_template_directory_uri() . '/build/style-index.css',
                [],
                $theme_version
            );
            wp_enqueue_style('eryndo-block-styles');

            // Load frontend scripts (for non-admin pages)
            if (! is_admin()) {
                // Register and enqueue image filter frontend script
                wp_register_script(
                    'eryndo-image-filter-frontend',
                    get_template_directory_uri() . '/assets/js/image-filter-frontend.js',
                    [],
                    $theme_version,
                    true
                );
                wp_enqueue_script('eryndo-image-filter-frontend');
            }

            // Load editor scripts (for admin pages)
            if (is_admin()) {
            $asset_path = get_template_directory() . '/build/index.asset.php';

            if (file_exists($asset_path)) {
                $asset_data = require $asset_path;

                // Register helper script
                wp_register_script(
                    'eryndo-helper-script',
                    get_template_directory_uri() . '/assets/js/helper.js',
                    [],
                    $theme_version,
                    true
                );

                // Register main editor script
                wp_register_script(
                    'eryndo-block-editor-script',
                    get_template_directory_uri() . '/build/index.js',
                    $asset_data['dependencies'],
                    $asset_data['version'],
                    true
                );

                // Enqueue scripts
                wp_enqueue_script('eryndo-helper-script');
                wp_enqueue_script('eryndo-block-editor-script');

                // Localize script with ZoloBlocks installer configuration
                wp_localize_script('eryndo-block-editor-script', 'eryndoParams', array(
                    'zolo_pro_status' => get_option('zolo_pro_status') ? 'active' : 'inactive',
                    'zoloblocks_installer' => array(
                        'ajax_url' => admin_url('admin-ajax.php'),
                        'nonce' => wp_create_nonce('eryndo_zoloblocks_nonce'),
                        'strings' => array(
                            'installing' => __('Installing ZoloBlocks plugin...', 'eryndo'),
                            'activating' => __('Activating ZoloBlocks plugin...', 'eryndo'),
                            'success' => __('ZoloBlocks plugin is ready!', 'eryndo'),
                            'reloading' => __('Reloading page to initialize ZoloBlocks plugin...', 'eryndo'),
                            'error' => __('Error: ', 'eryndo'),
                            'permission_error' => __('You do not have permission to install plugins.', 'eryndo'),
                            'plugin_required' => __('ZoloBlocks plugin is required to use the template library.', 'eryndo'),
                            'plugin_not_installed' => __('ZoloBlocks plugin is not installed. Would you like to install it now?', 'eryndo'),
                            'plugin_not_activated' => __('ZoloBlocks plugin is installed but not activated. Would you like to activate it now?', 'eryndo'),
                            'install_activate' => __('Install & Activate', 'eryndo'),
                            'activate' => __('Activate', 'eryndo'),
                            'cancel' => __('Cancel', 'eryndo')
                        )
                    )
                ));
                }
            }
        }
    }

    // Initialize the class
    new Eryndo_Block_Assets();
}
