<?php
declare( strict_types=1 );

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

/**
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'body {
--fibofilters--colors--base: var( --nv-primary-accent, var( --fibofilters--colors--base-org ) );
--fibofilters--colors--link: var( --nv-primary-accent, var( --fibofilters--colors--link-org ) );
			}'
		);
	}
);

/**
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<style>
			.widget li.fibofilters-item {
				margin-top: 0;
			}

			.nv-woo-filters {
				display: none !important;
			}
		</style>
		<?php
		$css = ob_get_clean();

		wp_add_inline_style(
			'fibofilters',
			str_replace( [ '<style>', '</style>' ], '', $css )
		);
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $products_per_page ): int {
		if ( ! defined( 'NEVE_PRO_VERSION' )
			|| (bool) ! get_option( 'nv_pro_woocommerce_booster_status', true )
		) {
			return (int) $products_per_page;
		}

		return (int) get_theme_mod( 'neve_products_per_page', 12 );
	}
);

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( ! defined( 'NEVE_PRO_VERSION' )
			|| (bool) ! get_option( 'nv_pro_woocommerce_booster_status', true )
		) {
			return $pagination_strategy;
		}

		$pagination = get_theme_mod( 'neve_shop_pagination_type', 'number' );

		if ( $pagination === 'number' ) {
			$pagination_strategy = 'pagination';
		}

		if ( $pagination === 'infinite' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);

/**
 * ╭────────────────────────────────────────────╮
 * | Neve Theme with Sparks addon Wishlist fix  |
 * ╰────────────────────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Break early if required modules are not active.
		if (
			! class_exists( '\Codeinwp\Sparks\Core\Sparks' ) &&
			! function_exists( 'sparks' )
		) {
			return;
		}

		$wishlist = sparks()->module( 'wish_list' );

		if ( ! $wishlist || ! (bool) $wishlist->should_load() ) {
			return;
		}

		ob_start();
		?>
		<script>
			(function () {
				if (typeof fiboFilters === 'undefined') {
					return;
				}

				function fibofiltersNeveWithSparksWishlistFix() {
					document.dispatchEvent(new Event('sparksWLInitNeeded'));
				}

				fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', fibofiltersNeveWithSparksWishlistFix);
				fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', fibofiltersNeveWithSparksWishlistFix);
			}());
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [], 'wishlist' );
	}
);

/**
 * ╭─────────────────────────────────────────────╮
 * | Neve Theme with Sparks addon Quick View fix |
 * ╰─────────────────────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Break early if required modules are not active.
		if (
			! class_exists( '\Codeinwp\Sparks\Core\Sparks' ) &&
			! function_exists( 'sparks' )
		) {
			return;
		}

		$quick_view = sparks()->module( 'quick_view' );

		if ( ! $quick_view || ! (bool) $quick_view->should_load() ) {
			return;
		}

		ob_start();
		?>
		<script>
			(function () {

				if (typeof fiboFilters === 'undefined') {
					return;
				}

				function fibofiltersNeveWithSparksQuickViewFix() {
					document.dispatchEvent(new CustomEvent('sparksQwInitNeeded'));
				}

				fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', fibofiltersNeveWithSparksQuickViewFix);
				fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', fibofiltersNeveWithSparksQuickViewFix);
			}());
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [], 'quick-view' );
	}
);

/**
 * ╭─────────────────────────────────────────────╮
 * | Remove Neve infinite scroll trigger element |
 * ╰─────────────────────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$pagination_strategy = $this->get_config_value( 'pagination_strategy' );
		if ( $pagination_strategy !== 'infinite-scroll' ) {
			return;
		}

		ob_start();
		?>
		<script>
			const fiboFiltersNeveInfiniteScrollTrigger = document.querySelector('.load-more-products');
			if (fiboFiltersNeveInfiniteScrollTrigger) {
				fiboFiltersNeveInfiniteScrollTrigger.remove();
			}
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [], 'infinite-scroll' );
	}
);
