<?php
/**
 * AssetLoader Utility Class
 *
 * Provides static methods to enqueue scripts and styles with WordPress.
 * Automatically handles .asset.php metadata files generated by build tools like wp-scripts.
 * Falls back to file modification time for versioning if asset metadata is not available.
 *
 * Usage:
 *     AssetLoader::enqueue_script( 'my-handle', 'my-script' );
 *     AssetLoader::enqueue_style( 'my-style', 'my-stylesheet' );
 *
 * @package RedditForWooCommerce\Utils
 */

namespace RedditForWooCommerce\Utils;

use RedditForWooCommerce\Config;

/**
 * Class AssetLoader
 *
 * A utility class for registering and enqueuing assets (scripts and styles)
 * in a WordPress plugin or theme, with support for asset metadata files.
 */
class AssetLoader {
	/**
	 * Enqueue a script using a script path and its asset metadata.
	 *
	 * @since 0.1.0
	 *
	 * @param string $handle    The handle for the script.
	 * @param string $file_name The script file name.
	 */
	public static function enqueue_script( $handle, $file_name ) {
		$script_path       = REDDIT_FOR_WOOCOMMERCE_PLUGIN_BUILD_PATH . $file_name;
		$script_url        = REDDIT_FOR_WOOCOMMERCE_BUILD_URL . $file_name . '.js';
		$script_asset_path = $script_path . '.asset.php';

		if ( file_exists( $script_asset_path ) ) {
			// phpcs:ignore Squiz.Commenting.InlineComment.InvalidEndChar
			$asset_data = require $script_asset_path; // nosemgrep
		} else {
			$asset_data = array(
				'dependencies' => array(),
				'version'      => file_exists( $script_path . '.js' ) ? filemtime( $script_path . '.js' ) : REDDIT_FOR_WOOCOMMERCE_VERSION,
			);
		}

		wp_enqueue_script(
			Config::ASSET_HANDLE_PREFIX . $handle,
			$script_url,
			$asset_data['dependencies'],
			$asset_data['version'],
			true
		);
	}

	/**
	 * Enqueue a style using a style path and its asset metadata.
	 *
	 * @since 0.1.0
	 *
	 * @param string $handle    The handle for the style.
	 * @param string $file_name The script file name.
	 */
	public static function enqueue_style( $handle, $file_name ) {
		$style_path       = REDDIT_FOR_WOOCOMMERCE_PLUGIN_BUILD_PATH . $file_name;
		$style_url        = REDDIT_FOR_WOOCOMMERCE_BUILD_URL . $file_name . '.css';
		$style_asset_path = $style_path . '.asset.php';

		if ( file_exists( $style_asset_path ) ) {
			// phpcs:ignore Squiz.Commenting.InlineComment.InvalidEndChar
			$asset_data = require $style_asset_path; // nosemgrep
		} else {
			$asset_data = array(
				'dependencies' => array(),
				'version'      => file_exists( $style_path . 'css' ) ? filemtime( $style_path . 'css' ) : REDDIT_FOR_WOOCOMMERCE_VERSION,
			);
		}

		wp_enqueue_style(
			Config::ASSET_HANDLE_PREFIX . $handle,
			$style_url,
			array(),
			$asset_data['version']
		);
	}

	/**
	 * Localize data for an enqueued script.
	 *
	 * This method allows passing PHP data to JavaScript using `wp_localize_script()`.
	 * It must be called after the script has been enqueued using `enqueue_script()`.
	 *
	 * @since 0.1.0
	 *
	 * @param string $handle     The script handle used in `enqueue_script()` (without prefix).
	 * @param string $object_name The name of the JavaScript object to contain the data.
	 * @param array  $data        The data to localize.
	 */
	public static function localize_script( $handle, $object_name, array $data ) {
		wp_localize_script(
			Config::ASSET_HANDLE_PREFIX . $handle,
			Config::AD_PARTNER_JS_VAR_PREFIX . $object_name,
			$data
		);
	}
}
