<?php
declare( strict_types=1 );

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

/*
 * ╭───────────────╮
 * | Theme filters |
 * ╰───────────────╯
 */
// 1. Necessary classes for product containers.
add_filter(
	'presscore_before_post_masonry-wrap_class',
	function ( $classes ) {
		$classes[] = 'product';
		$classes[] = 'post-' . get_the_ID();

		return $classes;
	}
);

// 2. Disabling option: Theme Options >> WooCommerce >> Product list >> Layout >> Collapse sidebar filters on mobile.
add_filter( 'dt_of_get_option-woocommerce_shop-sidebar-collapse', '__return_false' );

/*
 * ╭────────────╮
 * | Breakpoint |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/main_breakpoint',
	function ( $breakpoint ): int {
		if ( function_exists( 'of_get_option' ) ) {
			$breakpoint = of_get_option( 'sidebar-responsiveness' );
			$breakpoint = (int) str_replace( 'px', '', $breakpoint );
			$breakpoint--;
		}

		return (int) $breakpoint;
	}
);

/**
 * ╭────────────────────────────────────────────────────────╮
 * | Early product caching, before theme JavaScript actions |
 * ╰────────────────────────────────────────────────────────╯
 */
add_filter( 'fibofilters/config/early_products_caching', '__return_true' );

/**
 * ╭────────────────────────────╮
 * | Products fetching strategy |
 * ╰────────────────────────────╯
 */
add_filter( 'fibofilters/config/products_fetching_strategy', fn() => 'page' );

/**
 * ╭────────────────────────╮
 * | Products loop selector |
 * ╰────────────────────────╯
 */
add_filter(
	'fibofilters/config/products_loop_selectors',
	function ( $selectors ) {
		$selectors[] = '.dt-products.iso-container'; // Masonry.
		$selectors[] = '.dt-products.dt-css-grid-wrap .dt-css-grid'; // Grid.
		$selectors[] = '.dt-products.wc-layout-list'; // List.
		$selectors[] = '.elementor-widget-the7-wc-products .iso-container'; // Elementor, Products widget, Masonry.
		$selectors[] = '.elementor-widget-the7-wc-products .dt-css-grid'; // Elementor, Products widget, Grid.

		return $selectors;
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		ob_start();
		?>
		<div class="wf-cell fibofilters-product-placeholder">
			<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
		</div>
		<?php
		return (string) ob_get_clean();
	}
);

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

/*
 * ╭──────────────────────────╮
 * | Non-removable URL params |
 * ╰──────────────────────────╯
 */
add_filter(
	'fibofilters/config/non_removable_url_params',
	function ( $params ) {
		$params[] = 'wc_view_mode';

		return $params;
	}
);

/**
 * ╭──────────────────╮
 * | Frontend scripts |
 * ╰──────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersThe7Fix() {
					if (typeof jQuery !== 'undefined') {
						const the7Selectors = [
							'.dt-products.iso-container', // Masonry.
							'.dt-products.dt-css-grid-wrap .dt-css-grid', // Grid.
							'.dt-products.wc-layout-list', // List.
							'.elementor-widget-the7-wc-products .iso-container', // Elementor, Products widget, Masonry.
							'.elementor-widget-the7-wc-products .dt-css-grid', // Elementor, Products widget, Grid.
						];
						the7Selectors.forEach(function (selector) {
							const the7grid = jQuery(selector);
							if (the7grid.length && typeof the7grid.IsoLayzrInitialisation === 'function') {
								the7grid.IsoLayzrInitialisation();
								// Scroll to trigger animations on products.
								window.scrollBy(0, -1);
								window.scrollBy(0, 1);

								<?php // Some pagination types in the theme load all products on page load and then hide them. ?>
								the7grid.find('.hidden.wf-cell.product-wrap').removeClass('hidden').addClass('visible');
							}
						});
					}
				}

				fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', fibofiltersThe7Fix);
				fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', fibofiltersThe7Fix);

				<?php // Preserve view mode when fetching products. ?>
				fiboFilters.hooks.addFilter('fiboFilters.renderer.single_products_fetch_url', 'fibofilters', function (url) {
					const currentUrl = new URL(window.location.href);
					if (currentUrl.searchParams.has('wc_view_mode')) {
						url.searchParams.set('wc_view_mode', currentUrl.searchParams.get('wc_view_mode'));
					}
					return url
				});
			}
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [ 'jquery' ] );
	}
);

/*
 * ╭──────────────────────────────╮
 * | Show more products as button |
 * ╰──────────────────────────────╯
 */
add_filter( 'fibofilters/config/show_more_as_button', '__return_true' );

/*
 * ╭────────────╮
 * | Pagination |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		return wp_parse_args(
			[
				'type' => 'plain',
			],
			$args
		);
	}
);
