<?php
/**
 * Code4rest\Scripts\Component class
 *
 * @package code4rest
 */

namespace Code4rest\Scripts;

use Code4rest\Component_Interface;
use function Code4rest\code4rest;
use WP_Post;
use function add_action;
use function add_filter;
use function wp_enqueue_script;
use function get_theme_file_uri;
use function get_theme_file_path;
use function wp_script_add_data;
use function wp_localize_script;

/**
 * Class for adding scripts to the front end.
 */
class Component implements Component_Interface {

	/**
	 * Gets the unique identifier for the theme component.
	 *
	 * @return string Component slug.
	 */
	public function get_slug() : string {
		return 'scripts';
	}

	/**
	 * Adds the action and filter hooks to integrate with WordPress.
	 */
	public function initialize() {
		add_action( 'wp_enqueue_scripts', array( $this, 'action_enqueue_scripts' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'ie_11_support_scripts' ), 60 );
	}
	/**
	 * Add some very basic support for IE11
	 */
	public function ie_11_support_scripts() {
		if ( apply_filters( 'code4rest_add_ie11_support', false ) || code4rest()->option( 'ie11_basic_support' ) ) {
			wp_enqueue_style( 'code4rest-ie11', get_theme_file_uri( '/assets/css/ie.min.css' ), array(), CODE4REST_VERSION );
			wp_enqueue_script(
				'code4rest-css-vars-poly',
				get_theme_file_uri( '/assets/js/css-vars-ponyfill.min.js' ),
				array(),
				CODE4REST_VERSION,
				true
			);
			wp_script_add_data( 'code4rest-css-vars-poly', 'async', true );
			wp_script_add_data( 'code4rest-css-vars-poly', 'precache', true );
			wp_enqueue_script(
				'code4rest-ie11',
				get_theme_file_uri( '/assets/js/ie.min.js' ),
				array(),
				CODE4REST_VERSION,
				true
			);
			wp_script_add_data( 'code4rest-ie11', 'async', true );
			wp_script_add_data( 'code4rest-ie11', 'precache', true );
		}
	}
	/**
	 * Enqueues a script that improves navigation menu accessibility as well as sticky header etc.
	 */
	public function action_enqueue_scripts() {

		// If the AMP plugin is active, return early.
		if ( code4rest()->is_amp() ) {
			return;
		}

		$breakpoint = 1024;
		if ( code4rest()->sub_option( 'header_mobile_switch', 'size' ) ) {
			$breakpoint = code4rest()->sub_option( 'header_mobile_switch', 'size' );
		}
		// Enqueue the slide script.
		wp_register_script(
			'kad-splide',
			get_theme_file_uri( '/assets/js/splide.min.js' ),
			array(),
			CODE4REST_VERSION,
			true
		);
		wp_script_add_data( 'kad-splide', 'async', true );
		wp_script_add_data( 'kad-splide', 'precache', true );
		// Enqueue the slide script.
		wp_register_script(
			'code4rest-slide-init',
			get_theme_file_uri( '/assets/js/splide-init.min.js' ),
			array( 'kad-splide', 'code4rest-navigation' ),
			CODE4REST_VERSION,
			true
		);
		wp_script_add_data( 'code4rest-slide-init', 'async', true );
		wp_script_add_data( 'code4rest-slide-init', 'precache', true );
		wp_localize_script(
			'code4rest-slide-init',
			'code4restSlideConfig',
			array(
				'of'    => __( 'of', 'code4rest' ),
				'to'    => __( 'to', 'code4rest' ),
				'slide' => __( 'Slide', 'code4rest' ),
				'next'  => __( 'Next', 'code4rest' ),
				'prev'  => __( 'Previous', 'code4rest' ),
			)
		);
		if ( code4rest()->option( 'lightbox' ) ) {
			// Enqueue the lightbox script.
			wp_enqueue_script(
				'code4rest-simplelightbox',
				get_theme_file_uri( '/assets/js/simplelightbox.min.js' ),
				array(),
				CODE4REST_VERSION,
				true
			);
			wp_script_add_data( 'code4rest-simplelightbox', 'async', true );
			wp_script_add_data( 'code4rest-simplelightbox', 'precache', true );
			// Enqueue the slide script.
			wp_enqueue_script(
				'code4rest-lightbox-init',
				get_theme_file_uri( '/assets/js/lightbox-init.min.js' ),
				array( 'code4rest-simplelightbox' ),
				CODE4REST_VERSION,
				true
			);
			wp_script_add_data( 'code4rest-lightbox-init', 'async', true );
			wp_script_add_data( 'code4rest-lightbox-init', 'precache', true );
		}
		// Main js file.
		$file = 'navigation.min.js';
		// Lets make it possile to load a lighter file if things are not being used.
		if ( 'no' === code4rest()->option( 'header_sticky' ) && 'no' === code4rest()->option( 'mobile_header_sticky' ) && ! code4rest()->option( 'enable_scroll_to_id' ) && ! code4rest()->option( 'scroll_up' ) ) {
			$file = 'navigation-lite.min.js';
		}
		wp_enqueue_script(
			'code4rest-navigation',
			get_theme_file_uri( '/assets/js/' . $file ),
			array(),
			CODE4REST_VERSION,
			true
		);
		wp_script_add_data( 'code4rest-navigation', 'async', true );
		wp_script_add_data( 'code4rest-navigation', 'precache', true );
		wp_localize_script(
			'code4rest-navigation',
			'code4restConfig',
			array(
				'screenReader' => array(
					'expand'     => __( 'Expand child menu', 'code4rest' ),
					'expandOf'   => __( 'Expand child menu of', 'code4rest' ),
					'collapse'   => __( 'Collapse child menu', 'code4rest' ),
					'collapseOf' => __( 'Collapse child menu of', 'code4rest' ),
				),
				'breakPoints' => array(
					'desktop' => esc_attr( $breakpoint ),
					'tablet' => 768,
				),
				'scrollOffset' => apply_filters( 'code4rest_scroll_to_id_additional_offset', 0 ),
			)
		);
	}
}
