import {PixelioCartAbandonRecoveryMoreAjaxRequest} from "./components/pixelio_cart_abandon_recovery_more_ajax_request";
import {PixelioCartAbandonRecoveryMoreCartTimer} from "./components/pixelio_cart_abandon_recovery_more_cart_timer";
import {
	PixelioCartAbandonRecoveryMoreEmailPopUp
} from "../../../admin/assets/js/components/pixelio_cart_abandon_recovery_more_email_popup_on_add_to_cart";
import {
	PIXELIOCARTABANDONRECOVERYMOREEMAILID,
	PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS,
	PIXELIOCARTABANDONRECOVERYMORETIMER, PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS
} from "./var/pixelio_cart_abandon_recovery_more_variable";
import {
	PixelioCartAbandonRecoveryMoreEventHandler
} from "./components/pixelio_cart_abandon_recovery_more_event_handler";
import {
	PixelioCartAbandonRecoveryMoreBoughtPopup
} from "../../../admin/assets/js/components/pixelio_cart_abandon_recovery_more_bought_notification_on_product";

import  "../../../frontend/assets/js/components/pixelio_cart_abandon_recovery_more_recovery_notice_error";
import {
	PixelioCartAbandonRecoveryMoreDeliveryInput
} from "./components/pixelio_cart_abandon_recovery_more_input_delivery";
import {PixelioWishlistButton} from "./components/pixelio_cart_abandon_recovery_more_wishlist";
import {
	PixelioCartAbandonRecoveryMoreEventTracker
} from "./components/pixelio_cart_abandon_recovery_more_event_tracker";
import {
	PixelioCartAbandonRecoveryMoreEngagementTracker
} from "./components/pixelio_cart_abandon_recovery_more_engagement_tracker";
(function ($) {

	$(document).ready(function () {
		new PixelioCartAbandonRecoveryMoreEventHandler();

		const tracker =
			new PixelioCartAbandonRecoveryMoreEventTracker();

		const engagementTracker =
			new PixelioCartAbandonRecoveryMoreEngagementTracker(
				tracker
			);

		engagementTracker.init();

		var ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
		var eventHandler = new PixelioCartAbandonRecoveryMoreEventHandler();
		var pixelioCartAbandonRecoveryMoreBoughtPopup = new PixelioCartAbandonRecoveryMoreBoughtPopup(".pixelio-cart-abandon-recovery-more-show-others-buy-checkbox-preview", null, null, true);
		var deliveryInput = new PixelioCartAbandonRecoveryMoreDeliveryInput();
		var wishlist = new PixelioWishlistButton();
		var email = "";

		var pixelioCartAbandonRecoveryMoreCartTimer = new PixelioCartAbandonRecoveryMoreCartTimer();

		pixelioCartAbandonRecoveryMoreCartTimer.pixelioCartAbandonRecoveryMoreStartTimer();
		function checkAddToCart() {
			if (
				localStorage.getItem('pixelio_add_to_cart') === 'true' &&
				localStorage.getItem('exit_popup' + PIXELIOCARTABANDONRECOVERYMOREEMAILID) === null
			) {
				localStorage.setItem('pixelio_add_to_cart', 'false');
				new PixelioCartAbandonRecoveryMoreEmailPopUp(true,false).pixelioCartAbandonRecoveryMoreShow();
			}
		}
		localStorage.setItem(
			'pixelio_utm_source',
			pixelioGetUTMParam('utm_source')
		);

		localStorage.setItem(
			'pixelio_utm_campaign',
			pixelioGetUTMParam('utm_campaign')
		);

		localStorage.setItem(
			'pixelio_utm_content',
			pixelioGetUTMParam('utm_content')
		);
		checkAddToCart();
		$(document).on('click', 'button, a', function () {
			var $el = $(this);
			var isAddToCartClass = $el.hasClass('add_to_cart') || $el.hasClass('wc_add_to_cart');
			var isAddToCartURL = $el.is('a') && $el.attr('href') && $el.attr('href').indexOf('?add-to-cart=') !== -1;

			if (isAddToCartClass || isAddToCartURL) {
				var productId = $el.data('product_id') || ($el.attr('href')?.match(/add-to-cart=(\d+)/)?.[1] || null);
				var productName = $el.data('product_name') || $el.text().trim();

				localStorage.setItem('pixelio_add_to_cart', 'true');
				pixelioCartAbandonRecoveryMoreCartTimer.pixelioCartAbandonRecoveryMoreSetRemaining();
			}
		});



		$(document).on('click change', function (e) {
			// Cherche un input email (plusieurs cas WooCommerce)
			var $target = $(e.target);
			var $form = $target.closest('form');

			var $email = $form.find(
				'.wc-block-components-text-input.wc-block-components-address-form__email input, input#email, input[type="email"]'
			).first();

			if (!$email.length || !$email.val()) {
				return;
			}

			email = $email.val();

			// Vérifie si la checkbox RGPD existe
			var $checkbox = $form.find('#contact-namespace-marketing-opt-in');

			// Si elle existe → envoyer seulement si cochée
			if (pixelio_ajax.isRgpdActive) {
				if (!$checkbox.is(':checked')) {
					return; // pas de consentement → on sort
				}
			}

			// Sinon (pas de checkbox) → on envoie direct
			var data = new FormData();
			data.append('email', email);
			data.append('consent_status', $checkbox.length ? $checkbox.is(':checked') : true);
			data.append(
				'utm_source',
				localStorage.getItem('pixelio_utm_source') || ''
			);

			data.append(
				'utm_campaign',
				localStorage.getItem('pixelio_utm_campaign') || ''
			);

			data.append(
				'utm_content',
				localStorage.getItem('pixelio_utm_content') || ''
			);
			ajaxRequest.makeAjaxRequest("pixelio_cart_abandon_recovery_more_save_three", data,saved_successfully);
		});

		function saved_successfully(response) {
			localStorage.setItem("pixelio_email",email)
		}

		function pixelioGetUTMParam(param) {
			const params = new URLSearchParams(window.location.search);

			return params.get(param) || null;
		}
	});

})(jQuery);

