<?php

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

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

if (! class_exists('Axvart_Block_Assets')) {
    /**
     * Axvart_Block_Assets Class
     * 
     * Manages the loading of theme assets for blocks and editor.
     */
    class Axvart_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(
                'axvart-block-styles',
                get_template_directory_uri() . '/build/style-index.css',
                [],
                $theme_version
            );
            wp_enqueue_style('axvart-block-styles');

            // Load frontend scripts (for non-admin pages)
            if (! is_admin()) {
                // Register and enqueue image filter frontend script
                wp_register_script(
                    'axvart-image-filter-frontend',
                    get_template_directory_uri() . '/assets/js/image-filter-frontend.js',
                    [],
                    $theme_version,
                    true
                );
                wp_enqueue_script('axvart-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(
                        'axvart-helper-script',
                        get_template_directory_uri() . '/assets/js/helper.js',
                        [],
                        $theme_version,
                        true
                    );

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

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

    // Initialize the class
    new Axvart_Block_Assets();
}
