<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins;

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

/**
 * Class YITHWooCommerceWishlist
 *
 *  Plugin Name: YITH WooCommerce Wishlist
 *  Plugin URI: https://yithemes.com/themes/plugins/yith-woocommerce-wishlist/
 *  Version: 3.31.0
 */
class YITHWooCommerceWishlist extends AbstractPlugin {
	public function init(): void {
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_compatibility_script' ], 100 );
		add_action( 'wp', [ $this, 'enable_early_caching' ], 100 );
	}

	public function enqueue_compatibility_script() {
		$context = apply_filters( 'fibofilters/request/get_context', [] );

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

		/**
		 * Break early if YITH WooCommerce Wishlist >> Settings >> General >> "Enable AJAX loading" option is disabled.
		 */
		if ( get_option( 'yith_wcwl_ajax_enable', 'no' ) === 'no' ) {
			return;
		}

		$plugin_version = defined( 'YITH_WCWL_VERSION' ) ? YITH_WCWL_VERSION : '1.0.0';

		ob_start();
		if ( version_compare( $plugin_version, '4.0.0', '<' ) ) {
			/**
			 * We will use a trigger that is called by the "YITH Infinite Scrolling" plugin after loading the products.
			 */
			?>
			<script>
				if (typeof fiboFilters !== 'undefined') {
					fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', function () {
						if (typeof jQuery !== 'undefined') {
							jQuery(document).trigger('yith_infs_added_elem');
						}
					});
					fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', function () {
						if (typeof jQuery !== 'undefined') {
							jQuery(document).trigger('yith_infs_added_elem');
						}
					});
				}
			</script>
			<?php
		} else {
			?>
			<script>
				if (typeof fiboFilters !== 'undefined') {
					fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', function () {
						if (typeof wp.hooks.doAction === 'function') {
							wp.hooks.doAction('yith_wcwl_init_add_to_wishlist_components');
						}
					});
					fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', function () {
						if (typeof wp.hooks.doAction === 'function') {
							wp.hooks.doAction('yith_wcwl_init_add_to_wishlist_components');
						}
					});
				}
			</script>
			<?php
		}
		$js = ob_get_clean();

		wp_add_inline_script(
			'fibofilters',
			str_replace( [ '<script>', '</script>' ], '', $js )
		);
	}

	public function enable_early_caching() {
		$context = apply_filters( 'fibofilters/request/get_context', [] );

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

		/**
		 * Break early if YITH WooCommerce Wishlist >> Settings >> General >> "Enable AJAX loading" option is disabled.
		 */
		if ( get_option( 'yith_wcwl_ajax_enable', 'no' ) === 'no' ) {
			return;
		}

		$plugin_version = defined( 'YITH_WCWL_VERSION' ) ? YITH_WCWL_VERSION : '1.0.0';

		// Enable early products caching for YITH WooCommerce Wishlist 4.0.0 and newer.
		if ( version_compare( $plugin_version, '4.0.0', '>=' ) ) {
			add_filter( 'fibofilters/config/early_products_caching', '__return_true' );
		}
	}
}
