<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Draly_Dropshipping_Starter_Pluggable_Yith_Woocommerce_Wishlist' ) ) {
	class Draly_Dropshipping_Starter_Pluggable_Yith_Woocommerce_Wishlist {
		/**
		 * Variable to hold the initialization state.
		 *
		 * @var  boolean
		 */
		protected static $initialized = false;

		public static function initialize() {
			// Do nothing if pluggable functions already initialized.
			if ( self::$initialized ) {
				return;
			}
			add_filter( 'yith_wcwl_rendering_method', function () {
				return 'php-templates';
			}, 10, 3 );
			add_filter( 'yith_wcwl_wishlist_param', function () {
				return '';
			}, 0 );
			add_action( 'draly_dropshipping_starter_function_shop_loop_item_wishlist', array(
				__CLASS__,
				'draly_dropshipping_starter_render_wishlist_button'
			), 1 );
			$yith_wcwl_button_position = get_option( 'yith_wcwl_button_position' );
			if ( in_array( $yith_wcwl_button_position, [ 'after_add_to_cart', 'add-to-cart' ], true ) ) {
				add_action( 'wp_loaded', function () {
					remove_action(
						'woocommerce_single_product_summary',
						[ YITH_WCWL_Frontend::get_instance(), 'print_button' ],
						31
					);
				} );
				add_action( 'woocommerce_after_add_to_cart_button', array(
					__CLASS__,
					'draly_dropshipping_starter_render_wishlist_button'
				), 4 );
			}
			add_action( 'draly_dropshipping_starter_header_wishlist', array(
				__CLASS__,
				'draly_dropshipping_starter_header_wishlist'
			) );
			add_action( 'wp_ajax_draly_dropshipping_starter_update_wishlist_count', array(
				__CLASS__,
				'draly_dropshipping_starter_update_wishlist_count'
			) );
			add_action( 'wp_ajax_nopriv_draly_dropshipping_starter_update_wishlist_count', array(
				__CLASS__,
				'draly_dropshipping_starter_update_wishlist_count'
			) );
			// State that initialization completed.
			self::$initialized = true;
		}

		public static function draly_dropshipping_starter_render_wishlist_button() {
			echo do_shortcode( '[yith_wcwl_add_to_wishlist]' );
		}

		public static function draly_dropshipping_starter_header_wishlist() {
			$yith_wcwl_wishlist_page_id = get_option( 'yith_wcwl_wishlist_page_id' );
			$wishlist_url               = get_page_link( $yith_wcwl_wishlist_page_id );
			if ( $wishlist_url != '' ) : ?>
                <div class="block-wishlist block-woo">
                    <a class="block-link"
                       href="<?php echo ! empty( $wishlist_url ) ? esc_url( $wishlist_url ) : '#'; ?>">
		                <span class="block-icon">
		                    <svg width="30" height="30" viewBox="0 0 30 30" fill="none"
                                 xmlns="http://www.w3.org/2000/svg">
                            <path d="M20.87 3.75C18.315 3.75 16.1088 5.24625 15 7.42875C13.8912 5.24625 11.685 3.75 9.13 3.75C5.4675 3.75 2.5 6.82125 2.5 10.6012C2.5 14.3812 4.77125 17.8463 7.70625 20.6925C10.6413 23.5388 15 26.25 15 26.25C15 26.25 19.2175 23.5838 22.2937 20.6925C25.575 17.61 27.5 14.3925 27.5 10.6012C27.5 6.81 24.5325 3.75 20.87 3.75Z"
                                  stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
                                  stroke-linejoin="round"/>
                            </svg>
                            <span class="count"><?php echo wp_kses_post( yith_wcwl_count_products() ); ?></span>
		                </span>
                    </a>
                </div>
			<?php endif;
		}

		public static function draly_dropshipping_starter_update_wishlist_count() {
			$count = yith_wcwl_count_products();
			wp_send_json( [
				'count' => $count
			] );
		}
	}
}