<?php

/**
 * Class Hooks
 * 
 * @package ArraySubs\Features\CustomerPortal\Services
 * @since 1.0.0
 *
 * Handles the customer portal assets for the ArraySubs plugin.
 */

namespace ArraySubs\Features\CustomerPortal\Services;

use ArraySubs\Supports\Config;

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

class Hooks
{
    public static $loadable = true;

    /**
     * Constructor.
     * 
     * Initializes the class by registering WordPress hooks.
     */
    public function __construct()
    {
        add_action('wp_enqueue_scripts', [$this, 'enqueueAssets'], 100);
    }

    /**
     * Enqueues the required CSS and JavaScript files.
     *
     * @return void
     */
    public function enqueueAssets(): void
    {
        // Only load on WooCommerce customer account pages
        if (!function_exists('is_account_page') || !is_account_page()) {
            return;
        }

        // Load dependencies from webpack asset file
        $asset_file = Config::get('plugin.public_path') . 'build/customer-portal.asset.php';
        $asset = file_exists($asset_file) ? require $asset_file : ['dependencies' => ['wp-element'], 'version' => ARRAYSUBS_VERSION];

        // Enqueue customer portal CSS
        wp_enqueue_style(
            'arraysubs-customer-portal',
            Config::get('plugin.public_url') . 'build/customer-portal.css',
            [],
            $asset['version']
        );

        // Enqueue customer portal JS
        wp_enqueue_script(
            'arraysubs-customer-portal',
            Config::get('plugin.public_url') . 'build/customer-portal.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );

        // Load translations for React components
        wp_set_script_translations(
            'arraysubs-customer-portal',
            'arraysubs',
            Config::get('plugin.plugin_path') . 'languages'
        );
    }
}
