<?php

namespace Flare\ImageMap;

/**
 * Asset loader for assets generated by wp-scripts.
 */
class WpScriptsAsset {

	const ASSETDIR = 'assets/build/';

	/** @var string $path The base filename of the asset. */
	protected $path;

	/** @var string $handle The handle to use when registering a script or style. */
	protected $handle;

	/** @var array $meta Script meta generated by wp-scripts */
	protected $meta;

	/** @var string|false $version Version number used to enqueue scripts and styles. */
	protected $version;

	/** @var array<string> $script_deps Dependencies to register for this script on top of the ones generated by wp-scripts. */
	protected $style_deps;

	/** @var array<string> $script_deps Dependencies to register for this script on top of the ones generated by wp-scripts. */
	protected $script_deps;

	/**
	 * @param string        $path The base filename of the asset.
	 * @param string        $handle The handle to use when registering a script or style.
	 * @param string|false  $version Version number used to enqueue scripts and styles.
	 * @param array<string> $style_deps Dependencies to register for this stylesheet.
	 * @param array<string> $script_deps Dependencies to register for this script on top of the ones generated by wp-scripts.
	 */
	public function __construct(
		string $path,
		string $handle,
		$version = false,
		array $style_deps = array(),
		array $script_deps = array()
	) {
		$this->path        = $path;
		$this->handle      = $handle;
		$this->version     = $version;
		$this->style_deps  = $style_deps;
		$this->script_deps = $script_deps;
	}

	/**
	 * Get the url where the built scripts can be found.
	 *
	 * @param string $ext The extension of the file to return.
	 * @return string The url
	 * @since 0.1.0
	 **/
	public function get_url( $ext ) {
		return plugins_url( self::ASSETDIR . $this->path . ( $ext ? '.' . $ext : '' ), __DIR__ );
	}

	/**
	 * Get the path to the build folder.
	 *
	 * @param string $ext The extension of the file to return.
	 * @return string Path
	 * @since 0.1.0
	 **/
	public function get_path( $ext = null ) {
		return plugin_dir_path( __DIR__ ) . self::ASSETDIR . $this->path . ( $ext ? '.' . $ext : '' );
	}

	/**
	 * Load the dependencies from the loader file generated by wp-scripts.
	 *
	 * @return array The dependencies and version of the asset.
	 * @since 0.1.0
	 **/
	public function get_meta() {
		if ( ! $this->meta ) {
			$path = $this->get_path( 'asset.php' );

			if ( ! file_exists( $path ) ) {
				return array();
			}

			$this->meta = require $path;
		}

		return $this->meta;
	}

	/**
	 * Return the provided version or get the version from the meta file.
	 *
	 * @return string|false Asset version number.
	 * @since 0.1.0
	 **/
	public function get_version() {
		if ( ! $this->version ) {
			$meta = $this->get_meta();
			if ( ! $meta['version'] ) {
				return false;
			}

			return $meta['version'];
		}

		return $this->version;
	}

	/**
	 * Register the script.
	 *
	 * @return boolean Whether the registration was successful.
	 * @since 0.1.0
	 **/
	public function register_script() {
		$meta = $this->get_meta();
		if ( ! is_array( $meta['dependencies'] ) ) {
			return false;
		}

		$url = $this->get_url( 'js' );

		return wp_register_script(
			$this->handle,
			$url,
			array_merge( $this->script_deps, $meta['dependencies'] ),
			$meta['version'],
			true
		);
	}

	/**
	 * Register translations for the script.
	 *
	 * @since 0.1.6
	 **/
	public function register_translations() {
		wp_set_script_translations( $this->handle, Plugin::$slug );
	}

	/**
	 * Register the stylesheet.
	 *
	 * @return boolean Whether the registration was successful.
	 * @since 0.1.0
	 **/
	public function register_style() {
		$version = $this->get_version();
		if ( ! $version ) {
			return false;
		}

		if ( ! file_exists( $this->get_path( 'css' ) ) ) {
			return false;
		}

		wp_register_style(
			$this->handle,
			$this->get_url( 'css' ),
			$this->style_deps,
			$version
		);

		return true;
	}
}
