<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins;

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

/**
 * Class JVMWooCommerceWishlist
 *
 * Plugin Name: JVM WooCommerce Wishlist
 * Plugin URI: https://wordpress.org/plugins/jvm-woocommerce-wishlist/
 * Tested versions: 2.0.3
 */
class JVMWooCommerceWishlist extends AbstractPlugin {
	public function init(): void {
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_compatibility_script' ] );
	}

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

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

		ob_start();
		?>
		<script>
			(function ($) {
				$(document).ready(function () {
					if (typeof fiboFilters !== 'undefined') {
						fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', function () {
							if (typeof jQuery !== 'undefined' && typeof cix_wishlist_init === 'function') {
								jQuery(".jvm_add_to_wishlist, .wishlist-undo").off('click');
								cix_wishlist_init();
							}
						});
						fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', function () {
							if (typeof jQuery !== 'undefined' && typeof cix_wishlist_init === 'function') {
								jQuery(".jvm_add_to_wishlist, .wishlist-undo").off('click');
								cix_wishlist_init();
							}
						});
					}
				});
			}(jQuery));
		</script>
		<?php

		$this->enqueue_inline_script( (string) ob_get_clean(), [ 'jquery' ] );
	}
}
