<?php

namespace Gravity_Forms\Gravity_Tools\Assets;

class Asset_Processor {
	/**
	 * @var array $map - The Hash Map generated by our node scripts.
	 */
	private $js_map;

	/**
	 * @var string $asset_path - The path to the js dist directory.
	 */
	private $js_asset_path;

	/**
	 * @var string $match_pattern - The pattern to match against when identifying scripts to process.
	 */
	private $js_match_pattern;

	/**
	 * @var array $map - The Hash Map generated by our node scripts.
	 */
	private $css_map;

	/**
	 * @var string $asset_path - The path to the css dist directory.
	 */
	private $css_asset_path;

	/**
	 * @var string $match_pattern - The pattern to match against when identifying scripts to process.
	 */
	private $css_match_pattern;

	private $constant_name;

	/**
	 * Constructor
	 *
	 * @since 1.0
	 *
	 * @param array  $map
	 * @param string $asset_path
	 *
	 * @return void
	 */
	public function __construct( $js_map, $css_map, $js_asset_path, $css_asset_path, $js_match_pattern, $css_match_pattern, $constant_name ) {
		$this->js_map            = $js_map;
		$this->js_asset_path     = $js_asset_path;
		$this->js_match_pattern  = $js_match_pattern;
		$this->css_map           = $css_map;
		$this->css_asset_path    = $css_asset_path;
		$this->css_match_pattern = $css_match_pattern;
		$this->constant_name     = $constant_name;
	}

	/**
	 * Perform processing actions on assets.
	 *
	 * @since 1.0
	 *
	 * @return void
	 */
	public function process_assets() {
		$this->process_scripts();
		$this->process_styles();
	}

	/**
	 * Process the ver values for all of the registered scripts in order to append a
	 * file hash (if it exists) or the filemtime (if required).
	 *
	 * @since 1.0
	 *
	 * @return void
	 */
	private function process_scripts() {
		global $wp_scripts;

		$registered = $wp_scripts->registered;

		foreach ( $registered as &$asset ) {

			// Bail if not one of our assets.
			if ( strpos( $asset->src, $this->js_match_pattern ) === false ) {
				continue;
			}

			$basename = basename( $asset->src );
			$path     = sprintf( '%s/%s', $this->js_asset_path, $basename );

			// Asset doesn't exist in hash_map, skip.
			if ( ! array_key_exists( $basename, $this->js_map ) ) {
				continue;
			}

			// The hash is either the value from our map, or the filemtime for dev.
			$hash = defined( $this->constant_name ) && constant( $this->constant_name ) ?
				filemtime( $path ) :
				$this->js_map[ $basename ]['version'];

			$asset->ver = $hash;
		}

		$wp_scripts->registered = $registered;

		return;
	}

	/**
	 * Process the ver values for all of the registered scripts in order to append a
	 * file hash (if it exists) or the filemtime (if required).
	 *
	 * @since 1.0
	 *
	 * @return void
	 */
	private function process_styles() {
		global $wp_styles;

		$registered = $wp_styles->registered;

		foreach ( $registered as &$asset ) {

			// Bail if not one of our assets.
			if ( strpos( $asset->src, $this->css_match_pattern ) === false ) {
				continue;
			}

			$basename = basename( $asset->src );
			$path     = sprintf( '%s/%s', $this->css_asset_path, $basename );

			// Asset doesn't exist in hash_map, skip.
			if ( ! array_key_exists( $basename, $this->css_map ) ) {
				continue;
			}

			// The hash is either the value from our map, or the filemtime for dev.
			$hash = defined( $this->constant_name ) && constant( $this->constant_name ) ?
				filemtime( $path ) :
				$this->css_map[ $basename ]['version'];

			$asset->ver = $hash;
		}

		$wp_styles->registered = $registered;

		return;
	}
}