<?php
/**
 * Customiser Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\Errigal_WP_Hook as WP_Hook;
use Errigal\Customiser\Errigal_Custom_Typography as Typography;
use Errigal\Shared\Errigal_Asset as Asset;

/**
 * This class is in charge of adding customiser hooks.
 *
 * @since 1.1.0
 */
class Errigal_Customiser_Hooks extends Errigal_Hooks_Base {
	/**
	 * Stylesheet name: Customise controls.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const CUSTOMISE_CONTROLS = 'customise-controls';

	/**
	 * Enqueues customise preview assets.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/customize_preview_init/
	 *
	 * @return void
	 */
	public function enqueues_customise_preview_assets() {
		Asset::enqueue_style(
			'customise-preview-style',
			Asset::get_stylesheet_uri( 'customise-preview' )
		);

		$customise_preview_script_name = Asset::enqueue_script(
			'customise-preview',
			array(
				Asset::DEP_SCRIPT_CUSTOMIZE_PREVIEW,
			)
		);

		Asset::localise_script(
			$customise_preview_script_name,
			'ERRIGAL_FONT_NAMES',
			Typography::get_font_names()
		);

		Asset::localise_script(
			$customise_preview_script_name,
			'ERRIGAL_FONT_FAMILIES',
			Typography::get_font_families()
		);

		Asset::localise_script(
			$customise_preview_script_name,
			'ERRIGAL_FONT_SIZES',
			Typography::get_font_sizes()
		);
	}

	/**
	 * Enqueues customise control scripts.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/customize_controls_enqueue_scripts/
	 *
	 * @return void
	 */
	public function enqueue_customise_control_scripts() {
		Asset::enqueue_style(
			self::CUSTOMISE_CONTROLS,
			Asset::get_stylesheet_uri( self::CUSTOMISE_CONTROLS )
		);

		Asset::enqueue_script(
			'customise-preview-controls',
			array(
				Asset::DEP_SCRIPT_JQUERY,
				Asset::DEP_SCRIPT_CUSTOMIZE_CONTROLS,
			)
		);
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_action( WP_Hook::CUSTOMIZE_PREVIEW_INIT, array( $this, 'enqueues_customise_preview_assets' ) );
		add_action( WP_Hook::CUSTOMIZE_CONTROLS_ENQUEUE_SCRIPTS, array( $this, 'enqueue_customise_control_scripts' ) );
	}
}
