<?php

namespace GOSP\GravityOps\Core\Utils;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * AssetHelper provides utilities for handling script and style assets, including versioning
 * and structured array generation for use with GF Addons.
 */
class AssetHelper {

	/**
	 * Base URL of the plugin.
	 *
	 * @var string
	 */
	private $base_url = '';

	/**
	 * Base path of the plugin.
	 *
	 * @var string
	 */
	private $base_path = '';

	/**
	 * Constructor method.
	 *
	 * @param string $base_url The base URL for the application.
	 * @param string $base_path The base file path for the application.
	 *
	 * @return void
	 */
	public function __construct( $base_url, $base_path ) {
		$this->base_url  = $base_url;
		$this->base_path = $base_path;
	}

	/**
	 * Get version based on file modification time.
	 *
	 * @param string $file_path Absolute path to the file.
	 * @return string The timestamp version or default.
	 */
	public static function get_file_version( $file_path ) {
		return file_exists( $file_path ) ? date( 'ymd-Gis', filemtime( $file_path ) ) : '1.0.0';
	}

	/**
	 * Helper to quickly build the script array structure for GF Addons.
	 *
	 * @param string $handle The script handle.
	 * @param string $suffix The file suffix for versioning.
	 * @param array  $deps   Dependencies (e.g. ['jquery']).
	 * @param array  $enqueue_conditions GF Enqueue logic.
	 *
	 * @return array The formatted array for the GFAddon scripts() method.
	 */
	public function build_script( $handle, $suffix, $deps = [], $enqueue_conditions = [] ) {
		return [
			'handle'    => $handle,
			'src'       => $this->base_url . $suffix,
			'version'   => self::get_file_version( $this->base_path . $suffix ),
			'deps'      => $deps,
			'in_footer' => true,
			'enqueue'   => $enqueue_conditions,
		];
	}

	/**
	 * Helper to quickly build the style array structure for GF Addons.
	 *
	 * @param string $handle The style handle.
	 * @param string $suffix The file suffix for versioning.
	 * @param array  $enqueue_conditions GF Enqueue logic.
	 *
	 * @return array The formatted array for the GFAddon styles() method.
	 */
	public function build_style( $handle, $suffix, $enqueue_conditions = [] ) {
		return [
			'handle'  => $handle,
			'src'     => $this->base_url . $suffix,
			'version' => self::get_file_version( $this->base_path . $suffix ),
			'enqueue' => $enqueue_conditions,
		];
	}
}
