<?php
/**
 * Load scripts
 *
 * @package wpstream-theme
 */

add_action( 'wp_enqueue_scripts', 'wpstream_theme_enqueue_scripts', 1000 );
/**
 * Theme enqueue scripts
 */
function wpstream_theme_enqueue_scripts() {
	$suffix  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
	$bridge_url     = trailingslashit( get_template_directory_uri() ) . 'js/wpstream-playback-ui-bridge.js';
	$bridge_version = '1.0';

	if ( defined( 'WPSTREAM_PLUGIN_PATH' ) && defined( 'WPSTREAM_PLUGIN_DIR_URL' ) ) {
		$plugin_bridge_path = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/js/wpstream-playback-ui-bridge.js';
		if ( file_exists( $plugin_bridge_path ) ) {
			$bridge_url     = WPSTREAM_PLUGIN_DIR_URL . 'hello-wpstream/js/wpstream-playback-ui-bridge.js';
			$bridge_version = defined( 'WPSTREAM_PLUGIN_VERSION' ) ? WPSTREAM_PLUGIN_VERSION : gmdate( 'YmdHi', filemtime( $plugin_bridge_path ) );
		}
	}

	wp_enqueue_script( 'jquery' );
	// Check if the current page is a singular page.
	if ( is_singular() ) {
		// Enqueue FancyBox scripts and styles only when the condition is met.
		wp_enqueue_script( 'fancybox', trailingslashit( get_template_directory_uri() ) . 'fancybox/jquery.fancybox.pack.js', array( 'jquery' ), '1.0', true );
		wp_enqueue_style( 'fancybox', trailingslashit( get_template_directory_uri() ) . 'fancybox/jquery.fancybox.css', array(), '1.0', 'all' );
	}

	wp_enqueue_script( 'wpstream-theme-scripts', trailingslashit( get_template_directory_uri() ) . 'js/wpstream-theme-script.js', array( 'jquery' ), '1.0', true );
	wp_enqueue_script( 'wpstream-playback-ui-bridge', $bridge_url, array( 'jquery' ), $bridge_version, true );

	wp_localize_script(
		'wpstream-theme-scripts',
		'wpstreamThemeScriptsVars',
		array(
			'ajaxurl' 				=> 	admin_url( 'admin-ajax.php' ), // WordPress AJAX URL.
			'processing'			=>	esc_html('sending...','hello-wpstream'),
			'send_mess' 			=>  esc_html__('Send Message','hello-wpstream'),
			'is_user_logged_in' 	=> 	is_user_logged_in() ? '1' : '0',
			'comment_text_empty' 	=>  esc_html__('Please type your comment.','hello-wpstream'),
			'comment_author_empty' 	=> 	esc_html__('Please enter your name.', 'hello-wpstream'),
            'comment_email_empty' 	=> 	esc_html__('Please enter your email.', 'hello-wpstream'),
			'comment_email_invalid' => esc_html__('Please enter a valid email address.', 'hello-wpstream'),
			'gdpr_agree' 			=> esc_html__('You need to agree with GDPR terms.', 'hello-wpstream'),
		)
	);

	if ( wpstream_is_dashboard_page() ) {
		wp_enqueue_style( 'select2-css', trailingslashit( get_template_directory_uri() ) ."css/select2{$suffix}.css", array(), '4.1.0-rc.0' );
		wp_enqueue_style( 'select2-bootstrap-5-theme-css', trailingslashit( get_template_directory_uri() ) . "css/select2-bootstrap-5-theme{$suffix}.css", array(), '1.3.0' );

		// Scripts.
		wp_enqueue_script( 'select2-js',  trailingslashit( get_template_directory_uri() )."js/select2{$suffix}.js", array( 'jquery' ), '4.1.0-rc.0', true );
		wp_enqueue_script( 'jquery-ui-autocomplete' );
		wp_enqueue_style( 'jquery-ui', trailingslashit( get_template_directory_uri() ).'css/jquery-ui.css', array(), '1.0', 'all' );
	}
}