<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

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

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		$cols = get_theme_mod( 'shop_woocommerce_catalog_columns_desktop' );
		$rows = get_theme_mod( 'shop_woocommerce_catalog_rows' );

		if ( ! empty( $cols ) && intval( $cols ) > 0 && ! empty( $rows ) && intval( $rows ) > 0 ) {
			return intval( $cols ) * intval( $rows );
		}

		return 12;
	}
);

/**
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		$shop_archive_sidebar = get_theme_mod( 'shop_archive_sidebar', 'no-sidebar' );

		if ( $shop_archive_sidebar === 'sidebar-right' ) {
			return 'right';
		} elseif ( $shop_archive_sidebar === 'sidebar-left' || $shop_archive_sidebar === 'sidebar-slide' ) {
			return 'left';
		}

		return 'right';
	}
);

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( ! in_array( 'pagination_strategy', $this->args['integration_scope'], true ) ) {
			return $pagination_strategy;
		}

		$shop_archive_pagination_type = get_theme_mod( 'shop_archive_pagination_type', 'default' );

		if ( $shop_archive_pagination_type === 'default' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $shop_archive_pagination_type === 'load-more' ) {
			$pagination_strategy = 'show-more-button';
		} elseif ( $shop_archive_pagination_type === 'infinite-scroll' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);
add_action(
	'wp',
	function () {
		// Remove pagination container from theme.
		remove_action( 'woocommerce_after_shop_loop', 'botiga_woo_pagination_load_more_button' );
		remove_action( 'wp_enqueue_scripts', 'botiga_woo_pagination_enqueue_scripts', 10 );
	},
	20
);

/**
 * ╭───────────────────────────────────────────────────╮
 * | Compatibility with JS functions on the store page |
 * ╰───────────────────────────────────────────────────╯
 */
add_action(
	'wp',
	function () {
		// Swatches on shop catalog.
		$swatches_on_shop_catalog = false;
		if ( defined( 'BOTIGA_PRO_VERSION' ) && class_exists( 'Botiga_Modules' ) && Botiga_Modules::is_module_active( 'product-swatches' ) ) {
			$swatches_on_shop_catalog = get_theme_mod( 'product_swatch_on_shop_catalog', 0 ) === 1;
		}
		// Show quantity on shop catalog.
		$shop_product_quantity = defined( 'BOTIGA_PRO_VERSION' ) && get_theme_mod( 'shop_product_quantity', 0 ) === 1;
		// Quick view on shop catalog.
		$shop_product_quickview_layout = get_theme_mod( 'shop_product_quickview_layout', 'layout1' );

		if ( $swatches_on_shop_catalog ) {
			wp_enqueue_script( 'wc-add-to-cart-variation' );
		}

		// Wishlist.
		$wishlist_active = Botiga_Modules::is_module_active( 'wishlist' ) && get_theme_mod( 'shop_product_wishlist_layout', 'layout1' ) !== 'layout1';

		if (
			$swatches_on_shop_catalog
			|| $shop_product_quantity
			|| $shop_product_quickview_layout !== 'layout1'
			|| $wishlist_active
		) {
			// Cache page "body" before DOM ready.
			add_filter( 'fibofilters/config/early_products_caching', '__return_true' );

			add_action(
				'wp_enqueue_scripts',
				function () use ( $swatches_on_shop_catalog, $shop_product_quantity, $shop_product_quickview_layout, $wishlist_active ) {
					ob_start();
					?>
				<script>
					if (typeof fiboFilters !== 'undefined') {
						/**
						 * FIX: Prevent Botiga sidebar from closing when clicking inside FiboFilters.
						 */
						document.querySelector('.fibofilters-vertical-filters-container')?.addEventListener('click', function (e) {
							e.stopPropagation();
						});

						function fibofiltersBottigaFix() {
							<?php if ( $shop_product_quantity ) { ?>
							try {
								botiga.qtyButton.init();
							} catch (e) {
							}
							<?php } ?>

							<?php if ( $swatches_on_shop_catalog ) { ?>
							jQuery( '.variations_form' ).each( function() {
								jQuery( this ).wc_variation_form();
							});
							try {
								botiga.productSwatch.init();
								botiga.archiveProductSwatch.init();
							} catch (e) {
							}
							<?php } ?>

							<?php if ( $shop_product_quickview_layout !== 'layout1' ) { ?>
							try {
								botiga.quickView.init();
							} catch (e) {
							}
							<?php } ?>

							<?php if ( $wishlist_active ) { ?>
							try {
								botiga.wishList.init();
							} catch (e) {
							}
							<?php } ?>
						}

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