<?php

declare( strict_types=1 );

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

/*
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 * Claue -> Theme Options -> Color Scheme -> Primary Color
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$primary_color = cs_get_option( 'primary-color' );
		if ( empty( $primary_color ) ) {
			return;
		}

		wp_add_inline_style(
			'fibofilters',
			'body {
  --fibofilters--colors--base: ' . esc_attr( $primary_color ) . ';
  --fibofilters--colors--link: ' . esc_attr( $primary_color ) . ';
}'
		);
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Per Page
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $per_page ): int {
		if ( function_exists( 'cs_get_option' ) && intval( cs_get_option( 'wc-number-per-page' ) > 0 ) ) {
			$per_page = intval( cs_get_option( 'wc-number-per-page' ) );
		}

		return $per_page;
	}
);

/**
 * ╭──────────────────╮
 * | Products per row |
 * ╰──────────────────╯
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Number Of Column
 */
add_filter(
	'fibofilters/config/products_per_row',
	function ( $per_row ): int {
		if ( function_exists( 'cs_get_option' ) && intval( cs_get_option( 'wc-column' ) > 0 ) ) {
			$per_row = 12 / intval( cs_get_option( 'wc-column' ) );
		}

		return $per_row;
	}
);

/**
 * ╭─────────────────────────────────╮
 * | Disable result counter override |
 * ╰─────────────────────────────────╯
 * Instead of a counter, the theme has a Column Switcher and overriding it causes the switcher to work incorrectly.
 */
add_filter( 'fibofilters/config/override_result_count_enabled', '__return_false' );

/**
 * ╭─────────────────────────────────────────────────────────────────────╮
 * | Extending the list of items to be retained in the product container |
 * ╰─────────────────────────────────────────────────────────────────────╯
 * Element with class 'grid-sizer' is used by Isotope for calculating the size of grid items.
 */
add_filter(
	'fibofilters/config/clear_product_container_exceptions',
	function ( $selector ) {
		return $selector . ':not(.grid-sizer)';
	}
);

/*
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<style>
			.result-count-order {
				margin-bottom: 20px !important;
			}

			.jas-ajax-load {
				display: none;
			}

			.sidebar .widget ul li.fibofilters-item:before {
				display: none !important;
			}

			.sidebar .widget ul li.fibofilters-item {
				padding-left: 0;
				margin-bottom: 0;
			}
		</style>
		<?php
		$css = ob_get_clean();

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

add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom JS.
		ob_start();
		/**
		 * Theme uses Isotope for product layout: https://isotope.metafizzy.co/
		 * After filtering products, we need to reload Isotope layout including appended products.
		 */
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersFixColumns() {
					const colSwitch = jQuery('.wc-col-switch a.active');
					if (colSwitch.length === 1) {
						const col = colSwitch.data('col');
						jQuery('.products .product').removeClass('jas-col-md-2 jas-col-md-3 jas-col-md-4 jas-col-md-6').addClass('jas-col-md-' + col);
					}
				}

				function fibofiltersThemeFix() {
					if (typeof jQuery === 'undefined') {
						return;
					}

					const claueProductWrapper = jQuery('.jas-masonry.products');
					if (typeof claueProductWrapper.data('isotope') !== 'undefined') {
						claueProductWrapper.data('isotope').options.transitionDuration = 0;
						claueProductWrapper.imagesLoaded().progress(function () {
							fibofiltersFixColumns();
							claueProductWrapper.isotope('reloadItems').isotope('arrange');
						});
					} else {
						fibofiltersFixColumns();
					}
				}

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

			const fiboFiltersJasAjaxLoad = document.querySelector('.jas-ajax-load');
			if (fiboFiltersJasAjaxLoad) {
				fiboFiltersJasAjaxLoad.remove();
			}
		</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 strategy |
 * ╰─────────────────────╯
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Pagination Style
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Enable Infinite Scroll
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( ! function_exists( 'cs_get_option' ) ) {
			return $pagination_strategy;
		}

		$product_pagination_style = cs_get_option( 'wc-pagination', 'number' );

		if ( $product_pagination_style === 'number' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $product_pagination_style === 'loadmore' && cs_get_option( 'wc-scroll' ) ) {
			$pagination_strategy = 'infinite-scroll';
		} elseif ( $product_pagination_style === 'loadmore' ) {
			$pagination_strategy = 'show-more-button';
		}

		return $pagination_strategy;
	}
);

/**
 * ╭─────────────────────────────────────────────────────────────────╮
 * | Disabling tablet and mobile mode when Filter Switcher is active |
 * ╰─────────────────────────────────────────────────────────────────╯
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Enable Filter Switcher
 * Claue -> Theme Options -> Woocommerce -> Product Listing Settings -> Layout: No Sidebar
 */
add_action(
	'template_redirect',
	function () {
		if ( ! function_exists( 'cs_get_option' ) ) {
			return;
		}
		$layout         = cs_get_option( 'wc-layout' );
		$sidebar_filter = cs_get_option( 'wc-sidebar-filter' );

		if ( $layout === 'no-sidebar' && $sidebar_filter ) {
			add_filter( 'fibofilters/config/mobile_filters_enabled', '__return_false' );
			add_filter( 'fibofilters/config/tablet_filters_enabled', '__return_false' );
		}
	},
	5
);
