<?php
/**
 * Script loader and other essential things for theme
 * @since 1.0
 * @package RoyalNews
 */

if( !function_exists( 'royalnews_file_include' ) ){
	/**
	 * Load the files needed for theme
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_file_include( $files, $base = 'includes' ){
		foreach ( $files as $file ) {
			$path = $base . '/' . $file . '.php';
			require_once get_theme_file_path( $path );
		}
	}
}

if( !function_exists( 'royalnews_script_enqueue' ) ){
	/**
	 * Script loader
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_script_enqueue( $scripts ){
		foreach ( $scripts as $script ) {
			# Do not try to enqueue anything if handler is not supplied.
			if( ! isset( $script[ 'handle' ] ) ){
			    return;
			}
			$handle = $script[ 'handle' ];

			$version = false;
			if( isset( $script[ 'version' ] ) ){
			    $version = $script[ 'version' ];
			}

			$dependency = array();
			if( isset( $script[ 'dependency' ] ) ){
			    $dependency = $script[ 'dependency' ];
			}

			if( isset( $script[ 'style' ] ) ){
				$src = get_theme_file_uri( $script[ 'style' ] );
				if( isset( $script[ 'external' ] ) ){
					$src = $script[ 'style' ];
				}
				wp_enqueue_style( $handle, $src, $dependency, $version );
			}elseif( isset( $script[ 'script' ] ) ){
				$src = get_theme_file_uri( $script[ 'script' ] );
				if( isset( $script[ 'external' ] ) ){
					$src = $script[ 'script' ];
				}

				$in_footer = isset( $script[ 'footer' ] ) ? isset( $script[ 'footer' ] ) : false;

				$dependency[] = 'jquery';
				wp_enqueue_script( $handle, $src, $dependency, $version, $in_footer );

				if( isset( $script['localize'] ) && count( $script['localize'] ) > 0 ) {
					wp_localize_script( $script[ 'handle' ] , $script['localize']['key'] , $script['localize']['data'] );
				}
			}
		}
	}
}

if( !function_exists( 'royalnews_enqueue_scripts' ) ){	
	/**
	 * Enqueue Different Scripts for theme
	 * @since 1.0
	 * @package RoyalNews
	 */
	function royalnews_enqueue_scripts(){
		global $wp_query;
		royalnews_script_enqueue( array(
			array(
				'handle' => 'main-style',
				'style' => 'style.css'
			),
			array(
				'handle' => 'royalnews-style',
				'style' => 'assets/css/royalnews.css'
			),
			array(
				'handle' => 'slick',
				'style' => 'assets/third-party/slick/slick.css',
				'version' => '1.8.1'
			),
			array(
				'handle' => 'google-font',
				'style' => 'https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;1,100;1,200;1,300;1,400;1,500;1,600&family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;1,100;1,200;1,300;1,400;1,500&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;1,100;1,300;1,400;1,500;1,700&display=swap',
				'external' => true
			),
			array(
				'handle' => 'fontawesome',
				'style' => 'assets/third-party/fontawesome/css/all.min.css',
				'version' => '5.15.3'
			),
			array(
				'handle' => 'slick',
				'script' => 'assets/third-party/slick/slick.js',
				'version' => '1.8.1'
			),
			array(
				'handle' => 'sticky',
				'script' => 'assets/third-party/sticky/jquery-sticky.js',
				'version' => '1.0.4'
			),		
			array(
				'handle' => 'royalnews-script',
				'script' => 'assets/js/royalnews.js',
			)
		));

		# enqueue comment-reply.js in single page only
		if( ( is_single() || is_page() ) && comments_open() && get_option( 'thread_comments' ) ){
			wp_enqueue_script( 'comment-reply' );
		}
	}
}

if( !function_exists( 'royalnews_customizer_enqueue_scripts' ) ){
	/**
	 * Enqueue Different Scripts for customizer
	 * @since 1.1
	 * @package RoyalNews
	 */
	function royalnews_customizer_enqueue_scripts(){
		royalnews_script_enqueue( array(
			array(
				'handle' => 'royalnews-customizer-style',
				'style' => 'assets/css/royalnews-customizer.css',
			),
			array(
				'handle' => 'royalnews-customizer-script',
				'script' => 'assets/js/royalnews-customizer.js',
			)
		));
	}
}

/**
 * Needed Files for theme theme
 * @since 1.0
 * @package RoyalNews
 */
royalnews_file_include( array(
	'third-party/breadcrumb/init',
	'third-party/tgm/class-tgm-plugin-activation',
	'loader/essentials',
	'loader/hooks',
	'loader/class-meta-field',
	'loader/kirki-helper',
	'loader/timezones',
	'customizer/customizer-init',
));