<?php
declare( strict_types=1 );

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

/*
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 * Shopkeeper -> Styling
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$mods       = get_theme_mods();
		$main_color = $mods['main_color'] ?? '#f43f3f';

		$css = "
			body {
				--fibofilters--colors--link: {$main_color};
				--fibofilters--colors--base: {$main_color};
                --fibofilters--colors-input-border: var( --wc-form-border-color  );
			}
		";

		wp_add_inline_style( 'fibofilters', $css );
	}
);

/*
 * ╭─────────────────────────────────────────────────╮
 * | Hiding Shopkeeper off-canvas button when needed |
 * ╰─────────────────────────────────────────────────╯
 */
add_action(
	'template_redirect',
	function () {
		$tablet_filters_enabled = $this->get_config_value( 'tablet_filters_enabled' );
		$mobile_filters_enabled = $this->get_config_value( 'mobile_filters_enabled' );

		if ( $mobile_filters_enabled || $tablet_filters_enabled ) {
			add_action(
				'wp_enqueue_scripts',
				function () use ( $mobile_filters_enabled, $tablet_filters_enabled ) {
					// Custom CSS.
					ob_start();
					?>
				<style>
						<?php if ( $tablet_filters_enabled && $mobile_filters_enabled ) { ?>
						@media (min-width: 768px) and (max-width: 1024px) {
							#button_offcanvas_sidebar_left {
								display: none !important;
							}
						}
						<?php } ?>

						<?php if ( $tablet_filters_enabled && ! $mobile_filters_enabled ) { ?>
						@media (max-width: 1024px) {
							#button_offcanvas_sidebar_left {
								display: none !important;
							}
						}

						<?php } ?>

						<?php if ( $mobile_filters_enabled ) { ?>
						@media (max-width: 767px) {
							#button_offcanvas_sidebar_left {
								display: none !important;
							}
						}
						<?php } ?>
				</style>
					<?php
					$css = ob_get_clean();

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

/**
 * ╭─────────────────────────────╮
 * | Config: Pagination strategy |
 * ╰─────────────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		$pagination_type = get_theme_mod( 'pagination_shop', 'classic' );

		$map = [
			'classic'          => 'pagination',
			'load_more_button' => 'show-more-button',
			'infinite_scroll'  => 'infinite-scroll',
		];

		return $map[ $pagination_type ] ?? $pagination_strategy;
	}
);

/**
 * ╭───────────────────────────────╮
 * | Force use YITH WHISHLIST AJAX |
 * ╰───────────────────────────────╯
 *  The YITH WISHLIST plugin does not work correctly with FiboFilters when the Ajax mode option is not enabled.
 *  This information is stored in the database in options, and I am forcing its use here.
 */
add_filter(
	'option_yith_wcwl_ajax_enable',
	function ( $value ) {
		if ( class_exists( 'YITH_WCWL' ) ) {
			return 'yes';
		}

		return $value;
	}
);

/**
 * ╭───────────────────────────────────────────╮
 * | Disable Shopkeeper AJAX load for products |
 * ╰───────────────────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_dequeue_script( 'shopkeeper-wc-products-ajax-load' );
	},
	20
);

/**
 * ╭─────────────────────────────╮
 * | Fix product card animations |
 * ╰─────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersShopkeeperFix() {
					if (typeof product_card_animation === 'function') {
						product_card_animation(null, 200);
					}
				}

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