<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

/**
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		if ( ! function_exists( 'astra_get_option' ) ) {
			return;
		}
		$base_color = astra_get_option( 'theme-color' );
		$link_color = astra_get_option( 'base-link-colors-group' );
		wp_add_inline_style(
			'fibofilters',
			"body {
				--fibofilters--colors--base: $base_color;
				--fibofilters--colors--link: $link_color;
			}"
		);
	}
);

/**
 * ╭────────────╮
 * | Breakpoint |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/main_breakpoint',
	function (): int {
		if ( function_exists( 'astra_get_tablet_breakpoint' ) ) {
			return (int) astra_get_tablet_breakpoint();
		} else {
			return 921;
		}
	}
);

/**
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		if ( ! function_exists( 'astra_page_layout' ) ) {
			return 'right';
		}
		$page_layout = astra_page_layout();
		if ( $page_layout === 'right-sidebar' ) {
			return 'right';
		} elseif ( $page_layout === 'left-sidebar' ) {
			return 'left';
		}

		return 'right';
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		if ( function_exists( 'astra_get_option' ) ) {
			return (int) astra_get_option( 'shop-no-of-products' );
		}

		return 12;
	}
);

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

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( class_exists( 'Astra_Ext_Extension' )
			&& function_exists( 'astra_get_option' )
			&& \Astra_Ext_Extension::is_active( 'woocommerce' )
		) {
			$shop_pagination = astra_get_option( 'shop-pagination' );

			if ( $shop_pagination === 'number' ) {
				$pagination_strategy = 'pagination';
			} elseif ( $shop_pagination === 'infinite' ) {
				$type = astra_get_option( 'shop-infinite-scroll-event' );
				if ( $type === 'click' ) {
					$pagination_strategy = 'show-more-button';
				} elseif ( $type === 'scroll' ) {
					$pagination_strategy = 'infinite-scroll';
				}
			}
		}

		return $pagination_strategy;
	}
);


/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			.site-main .ast-shop-pagination-infinite {
				display: none;
			}
		</style>
		<?php
		$css = ob_get_clean();

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

/**
 * ╭──────────────────────╮
 * | "Reveal Effect" fix  |
 * ╰──────────────────────╯
 *  When "Reveal Effect" for shop catalog is, the products grid can't be loaded.
 *  The option can be found in:
 *  Customizing >> WooCommerce Product Catalog >> Shop structure options >> Reveal Effect.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Break early if required Astra modules are not active.
		if (
			! class_exists( 'Astra_Ext_WooCommerce' )
			|| ! class_exists( 'Astra_Ext_Blog_Pro' )
		) {
			return;
		}

		/**
		 * Starting the fade in effect after loading products when the "Reveal Effect" option is active.
		 *
		 * See: Customizing >> WooCommerce Product Catalog >> Shop structure options >> Reveal Effect
		 */
		if (
			! function_exists( 'astra_addon_check_reveal_effect_condition' )
			|| ! astra_addon_check_reveal_effect_condition( 'woocommerce' )
		) {
			return;
		}

		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersAstraFix() {
					if (typeof fadin === 'function') {
						fadin('.ast-fade-up', {delay: 200});
					}
				}

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

/**
 * ╭──────────────────────────────╮
 * | "Reveal Effect" fix - case 2 |
 * ╰──────────────────────────────╯
 *  First see through "Reveal Effect" fix" above.
 *  There are the second option the "Reveal Effect" for blog archives. Even if the "Reveal Effect" for shop is disabled,
 *  products on some views might get a ".ast-fade-up" class because "astra_addon_check_reveal_effect_condition( 'blog' )"
 *  returns true on a search page.
 *
 *  How to recreate it:
 *  1. Enable Astra Pro Modules: "Blog Pro" and "WooCommerce"
 *  2. Disable the option "Reveal Effect" in Customizing >> WooCommerce Product Catalog >> Shop structure options
 *  3. Enable the option "Reveal Effect" in Blog >> Blog / Archive >> Blog Layout
 *  4. A product grid will be buggy on a WooCommerce search results page and for archives pages with a "?s=" param.
 *
 *  The solution is force disabling the option "Reveal Effect" for Blog when "Reveal Effect" for Shop is disabled
 *  and FiboFilters should be displayed.
 */
add_filter(
	'astra_get_option_blog-reveal-effect',
	function ( $value ) {
		$context = apply_filters( 'fibofilters/request/get_context', [ 'show_filters' => false ] );

		if (
		! empty( $context['show_filters'] ) && $context['show_filters']
		&& function_exists( 'astra_get_option' )
		&& ! astra_get_option( 'shop-reveal-effect' )
		) {

			$value = false;
		}

		return $value;
	}
);

/**
 * ╭──────────────────────╮
 * | Astra Quick view fix |
 * ╰──────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Break early if required Astra modules are not active.
		if ( ! ( class_exists( 'Astra_Ext_Extension' )
				&& function_exists( 'astra_get_option' )
				&& \Astra_Ext_Extension::is_active( 'woocommerce' ) )
		) {
			return;
		}

		$quick_view = astra_get_option( 'shop-quick-view-enable' );

		if ( empty( $quick_view ) || $quick_view === 'disabled' ) {
			return;
		}

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

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

				function fibofiltersAstraQuickViewFix() {
					if (typeof AstraProQuickView === 'object') {
						AstraProQuickView.init();
					}
				}

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

/**
 * ╭─────────────────────────────────────╮
 * | Remove Astra infinite scroll script |
 * ╰─────────────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$context = apply_filters( 'fibofilters/request/get_context', [] );

		if ( empty( $context['show_filters'] ) ) {
			return;
		}

		wp_dequeue_script( 'astra-shop-pagination-infinite' );
		wp_deregister_script( 'astra-shop-pagination-infinite' );
	},
	100
);
