<?php
/**
 * Style loading functions
 *
 * @package Multi
 */

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

/**
 * Enqueues scripts
 */
function multimagazine_scripts() {
	$debug_suffix = WP_DEBUG ? '?v=' . time() : '';
	$debug_suffix = '?v=' . time();

	$script_dir = get_template_directory_uri() . '/js';

	wp_enqueue_script( 'multimagazine-basic', $script_dir . '/multimagazine.min.js' . $debug_suffix, array(), 1, true );
	$menu_animation_follow = get_theme_mod( 'multi_header_follow_mouse_mode', MULTIMAGAZINE_HEADER_FOLLOW_MOUSE_MODE );
	if ( ! empty( $menu_animation_follow ) && 'off' !== $menu_animation_follow ) {
		wp_enqueue_script( 'multimagazine-magic-line', $script_dir . '/multimagazine-magic-line.min.js' . $debug_suffix, array(), 1, true );
	}

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

	if ( is_singular() && 'list' === get_theme_mod( 'multi_related_layout', MULTIMAGAZINE_RELATED_LAYOUT ) ) {
		wp_enqueue_script( 'multimagazine-drag-drop-scroll', $script_dir . '/multimagazine-drag-drop-scroll.min.js' . $debug_suffix, array(), 1, true );
	}

	if ( is_single() && get_theme_mod( 'multi_load_related' ) ) {
		wp_enqueue_script( 'multimagazine-images-loaded', $script_dir . '/lib/images-loaded.js', array( 'jquery' ), 1, true );
		wp_enqueue_script( 'multimagazine-load-related', $script_dir . '/multimagazine-load-related.min.js' . $debug_suffix, array( 'jquery', 'multimagazine-images-loaded' ), 1, true );
	}

	if ( ( ! is_singular() || false !== strpos( get_page_template(), 'post-list' ) ) && get_theme_mod( 'multi_load_postlist', MULTIMAGAZINE_LOAD_POSTLIST ) !== 'off' ) {
		wp_enqueue_script( 'multimagazine-images-loaded', $script_dir . '/lib/images-loaded.js', array( 'jquery' ), 1, true );
		if ( is_home() || is_archive() ) {
			$post_list_layout = absint( get_theme_mod( 'multi_post_list_layout', MULTIMAGAZINE_POST_LIST_LAYOUT ) );
		} elseif ( is_search() ) {
			$post_list_layout = absint( get_theme_mod( 'multi_search_results_layout', MULTIMAGAZINE_SEARCH_RESULTS_LAYOUT ) );
		}
		if ( ! empty( $post_list_layout ) && in_array( $post_list_layout, array( 6, 7, 8 ), true ) ) {
			$deps = array( 'jquery', 'multimagazine-masonry' );
		} else {
			$deps = array( 'jquery' );
		}
		wp_enqueue_script( 'multimagazine-postlist-async-pagination', $script_dir . '/multimagazine-post-list-pagination.min.js' . $debug_suffix, $deps, time(), true );
	}

	if ( is_home() || is_search() || is_archive() ) {
		if ( is_home() || is_archive() ) {
			$post_list_layout = absint( get_theme_mod( 'multi_post_list_layout', MULTIMAGAZINE_POST_LIST_LAYOUT ) );
		} elseif ( is_search() ) {
			$post_list_layout = absint( get_theme_mod( 'multi_search_results_layout', MULTIMAGAZINE_SEARCH_RESULTS_LAYOUT ) );
		}
		if ( ! empty( $post_list_layout ) && in_array( $post_list_layout, array( 6, 7, 8 ), true ) ) {

			wp_enqueue_script( 'multimagazine-masonry', $script_dir . '/multimagazine-masonry.min.js' . $debug_suffix, array( 'masonry' ), time(), true );
		}
	}

	if ( is_singular() || class_exists( 'MultiBuilder' ) ) {
		wp_enqueue_script( 'multimagazine-glightbox', $script_dir . '/lib/glightbox.min.js', array(), 1, true );
		wp_enqueue_script( 'multimagazine-lightbox', $script_dir . '/multimagazine-lightbox.min.js' . $debug_suffix, array( 'multimagazine-glightbox' ), 1, true );
	}

	if ( multimagazine_slider_in_use() ) {
		wp_enqueue_script( 'multimagazine-splide', $script_dir . '/lib/splide.js', array(), 1, true );
		wp_enqueue_script( 'multimagazine-sliders', $script_dir . '/multimagazine-slider.min.js' . $debug_suffix, array( 'multimagazine-splide' ), 1, true );
	}

	if ( is_single() && get_theme_mod( 'multi_post_header', MULTIMAGAZINE_POST_HEADER ) ) {
		wp_enqueue_script( 'multimagazine-single-post-header', $script_dir . '/multimagazine-single-post-header.min.js' . $debug_suffix, array(), 1, true );
	}

	if ( get_theme_mod( 'multi_preloader', MULTIMAGAZINE_PRELOADER ) ) {
		wp_enqueue_script( 'multimagazine-preloader', $script_dir . '/multimagazine-preloader.min.js' . $debug_suffix, array(), 1, true );
	}

	if ( absint( get_theme_mod( 'multi_cursor', MULTIMAGAZINE_CURSOR ) ) !== 0 ) {
		wp_enqueue_script( 'multimagazine-cursor', $script_dir . '/multimagazine-cursor.min.js' . $debug_suffix, array(), 1, true );
	}

	if ( class_exists( 'WooCommerce' ) ) {
		wp_enqueue_script( 'multimagazine-glightbox', $script_dir . '/lib/glightbox.min.js', array(), 1, true );
		wp_enqueue_script( 'multimagazine-woocommerce', $script_dir . '/multimagazine-woocommerce.min.js' . $debug_suffix, array( 'multimagazine-glightbox' ), 1, true );
	}

	if ( get_theme_mod( 'multi_preloader_spinner_type', MULTIMAGAZINE_PRELOADER_SPINNER_TYPE ) === 'lottie' ) {
		wp_enqueue_script( 'multimagazine-lottie-player', $script_dir . '/lib/lottie-player.min.js', array(), 1, true );
	}
}

/**
 * Checks if slider is in use
 */
function multimagazine_slider_in_use() {
	// Do related/latest/featured use sliders?
	$related_slider = 'slider' === get_theme_mod( 'multi_related_layout', MULTIMAGAZINE_RELATED_LAYOUT );
	$top_slider     = get_theme_mod( 'multi_top_slider', MULTIMAGAZINE_TOP_SLIDER );
	if ( $related_slider || ( 'off' !== $top_slider && ! empty( $top_slider ) ) ) {
		return true;
	}

	// Is Woocommerce active and does it use slider?
	if ( class_exists( 'WooCommerce' ) && 'slider' === apply_filters( 'multimagazine_related_products_layout', 'list' ) ) {
		return true;
	}

	// Does any page or post has top slider?
	$options = array(
		'post_status' => 'publish',
		'meta_query'  => array( //phpcs:ignore
			'relation' => 'AND',
			array(
				'key'     => 'multi_show_top_slider',
				'value'   => array( 'latest', 'related', 'featured' ),
				'compare' => 'IN',
			),
			array(
				'key'     => 'multi_show_top_slider',
				'compare' => 'EXISTS',
			),
		),
	);

	$query = new WP_Query( $options );

	if ( 0 !== $query->found_posts ) {
		while ( $query->have_posts() ) {
			$query->the_post();
		}
		return true;
	}

	return false;
}

