<?php
/**
 * Theme script and style enqueue handling.
 *
 * @package Eronex
 * @since   0.0.1
 */

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

/**
 * Registers and enqueues front-end and Customizer assets.
 *
 * @package Eronex
 * @since   0.0.1
 */
if ( ! class_exists( 'Eronex_Enqueues' ) ) {

	/**
	 * Handles enqueuing of theme styles and scripts.
	 *
	 * @package Eronex
	 * @since   0.0.1
	 */
	class Eronex_Enqueues {

		/**
		 * Singleton instance.
		 *
		 * @since 0.0.1
		 * @var Eronex_Enqueues|null
		 */
		private static $instance;

		/**
		 * Filename suffix for minified assets (e.g. `.min` or empty).
		 *
		 * @since 0.0.1
		 * @var string
		 */
		private $minified;

		/**
		 * Returns the singleton instance.
		 *
		 * @since 0.0.1
		 *
		 * @return Eronex_Enqueues Instance of the class.
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor. Registers enqueue hooks for front end and Customizer.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function __construct() {
			$this->minified = '';

			// Frontend enqueues
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueues' ) );

			// Customize control enqueues
			add_action( 'customize_controls_enqueue_scripts', array( $this, 'customizer_enqueues' ) );
		}

		/**
		 * Enqueues scripts and styles for the public area.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function frontend_enqueues() {

			$assets_dir = ERONEX_DIR . 'assets/';
			$assets_uri = ERONEX_URI . 'assets/';

			$version = static function ( $file ) use ( $assets_dir ) {
				$file = $assets_dir . $file;

				return file_exists( $file ) ? filemtime( $file ) : ERONEX_VERSION;
			};

			// Vendor CSS.
			wp_enqueue_style(
				'eronex-hero-icons',
				$assets_uri . "vendor/css/hero-icons{$this->minified}.css",
				array(),
				$version( "vendor/css/hero-icons{$this->minified}.css" )
			);

			// Theme CSS.
			wp_enqueue_style(
				'eronex-theme',
				$assets_uri . "css/theme{$this->minified}.css",
				array(),
				$version( "css/theme{$this->minified}.css" )
			);

			// Theme RTL.
			if ( is_rtl() ) {
				wp_enqueue_style(
					'eronex-theme-rtl',
					$assets_uri . "css/theme-rtl{$this->minified}.css",
					array( 'eronex-theme' ),
					$version( "css/theme-rtl{$this->minified}.css" )
				);
			}

			// Vendor JS.
			wp_enqueue_script(
				'eronex-sliding-menu',
				$assets_uri . "vendor/js/sliding-menu{$this->minified}.js",
				array( 'jquery' ),
				$version( "vendor/js/sliding-menu{$this->minified}.js" ),
				true
			);

			// Theme JS.
			wp_enqueue_script(
				'eronex-theme',
				$assets_uri . "js/theme{$this->minified}.js",
				array( 'jquery' ),
				$version( "js/theme{$this->minified}.js" ),
				true
			);

			// Comments JS.
			if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
				wp_enqueue_script( 'comment-reply' );
			}
		}

		/**
		 * Enqueues styles and scripts for the Customizer controls.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function customizer_enqueues() {
			wp_enqueue_style( 'eronex-customizer', ERONEX_URI . "assets/admin/css/customizer{$this->minified}.css", array(), ERONEX_VERSION );
			wp_enqueue_script( 'eronex-customizer', ERONEX_URI . "assets/admin/js/customizer{$this->minified}.js", array( 'jquery', 'customize-controls' ), ERONEX_VERSION, true );
		}
	}
}

// Initiated Class
Eronex_Enqueues::get_instance();
