<?php
namespace ARCHM\Helpers;

class AssetsRegister {
	/**
	 * Initialize the class
	 *
	 * @author Siam Maruf
	 * @since 1.0
	 */
	public function __construct( $scripts, $styles ) {
		$this->register_scripts($scripts);
		$this->register_style($styles);
	}

	/**
	 * Register Scripts
	 *
	 * @param $scripts
	 * @return void
	 * @author Siam Maruf
	 */
	// phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
	public function register_scripts( $scripts ): void {
		foreach ( $scripts as $handler => $script ) {
			$deps       = $script['deps'] ?? false; // phpcs:ignore PHPCompatibility.Operators.NewOperators.t_coalesceFound
			$in_footer  = $script['in_footer'] ?? false; // phpcs:ignore PHPCompatibility.Operators.NewOperators.t_coalesceFound
			$version    = $script['version'] ?? false; // phpcs:ignore PHPCompatibility.Operators.NewOperators.t_coalesceFound

			wp_enqueue_script( $handler, $script['src'], $deps, $version, $in_footer );
			if ( $script['localize'] ) {
				if ( 'archive-status-notice' === $handler ) {
					wp_localize_script($handler,'archm_admin',$script['localize']);
				} else {
					wp_localize_script($handler,str_replace('-', '', $handler) . 'Obj',$script['localize']);
				}
			}
		}
	}

	/**
	 * Register Styles
	 *
	 * @param $styles
	 * @return void
	 * @author Siam Maruf
	 * @since 1.0
	 */
	// phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
	public function register_style( $styles ): void {
		foreach ( $styles as $handler => $style ) {
			$deps = $script['deps'] ?? false; // phpcs:ignore PHPCompatibility.Operators.NewOperators.t_coalesceFound

			wp_enqueue_style( $handler, $style['src'], $deps, ARCHM_PLUGIN_VERSION );
		}
	}
}
