import {PixelioCartAbandonRecoveryMoreColorPickers} from "./pixelio_cart_abandon_recovery_more_color_pickr";
import {PixelioCartAbandonRecoveryMoreTimer} from "./pixelio_cart_abandon_recovery_more_timer";
import {
	PixelioCartAbandonRecoveryMoreAjaxRequest
} from "../../../../frontend/assets/js/components/pixelio_cart_abandon_recovery_more_ajax_request";

const {
	PIXELIOCARTABANDONRECOVERYMOREEDITCONTAINERCLASS,
	PIXELIOCARTABANDONRECOVERYMOREHIDEIT,
	PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS
} = require("../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable");

export class PixelioCartAbandonRecoveryMorePopUp extends PixelioCartAbandonRecoveryMoreColorPickers {

	constructor(className, onShowCallback = null, onHideCallback = null, isFrontend = false, positiveClickCallback = null, startTimer = true, demo = false) {
		super();
		if(pixelio_ajax.isCheckoutOrCart && !demo){
			return;
		}

		this.className = className;
		this.demo = demo;
		this.onHideCallback = onHideCallback;
		this.onShowCallback = onShowCallback;
		this.positiveClickCallback = positiveClickCallback;
		this.startTimer = startTimer;
		this.ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
		this.yesButtonSelector = ".pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-yes-button-state ";
		this.inactivityTimeout = null;
		this.#pixelioCartAbandonRecoveryMoreCreateColorInit();
		this.#pixelioCartAbandonRecoveryMoreInitElements();
		this.pixelioCartAbandonRecoveryMoreInitTimer(startTimer);
		this.#pixelioCartAbandonRecoveryMoreCloseIconClick();
		this.#pixelioCartAbandonRecoveryMoreInitColor();
		this.#pixelioCartAbandonRecoveryMoreDisableBackgroundOfPreview();
		this.pixelioCartAbandonRecoveryMoreInitEvent();
		this.pixelioCartAbandonRecoveryMoreInitFrontendCall(isFrontend);

		if (this.demo) this.pixelioCartAbandonRecoveryMoreShow();
		this.exitIntentedPopupActionInit();
	}

	trackPage(page) {
		let pages = JSON.parse(localStorage.getItem(this.PAGES_KEY)) || [];
		if (pages[pages.length - 1] !== page) {
			pages.push(page);
		}
		if (pages.length > 50) pages.shift();
		localStorage.setItem(this.PAGES_KEY, JSON.stringify(pages));
		this.calculateExitScore(pages);
	}
	calculateExitScore(pages) {
		let uniquePages = [...new Set(pages)];
		let score = uniquePages.length; // +1 par page unique

		// bonus si panier / checkout visité
		if (pages.includes('cart') || pages.includes('checkout')) score += 2;

		// pages répétées
		let repeats = pages.length - uniquePages.length;
		if (repeats > 2) score += 1;

		// seuil pour déclencher popup
		if (score >= 5) this.pixelioCartAbandonRecoveryMoreShow();
	}
	resetInactivity() {
		var self = this;
		clearTimeout(this.inactivityTimeout);
		this.inactivityTimeout  = setTimeout(() => {
			self.pixelioCartAbandonRecoveryMoreShow();
		}, 15000); // 15s
	}
	exitIntentedPopupActionInit() {

		if (pixelio_ajax && pixelio_ajax.exit_popup && this.className &&  this.className.includes("exit") && !window.location.href.includes("admin")) {
			this.currentPage = history.state && history.state.page ? history.state.page : 'home';
			//this.trackPage(this.currentPage);
			if (history.state && history.state.page) {
				//this.trackPage(history.state.page);
			}
			if (!history.state || history.length <= 1) {
				const popupState = { page: 'pixelio-popup' };
				history.replaceState(popupState, '', location.href);
				history.pushState({ page: this.currentPage }, '', location.href);
			}
			this.pixelioCartAbandonRecoveryMoreInitScrollPercent();
			const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
			let lastY = window.scrollY;
			jQuery(window).on("scroll.pixelioExitScroll", function () {
				if (lastY - window.scrollY > 50) {
					if (isMobile) {
						self.pixelioCartAbandonRecoveryMoreShow();
					}
				}
				lastY = window.scrollY;
			});
			jQuery(document).on('touchstart.pixelio scroll.pixelioExitScroll', this.resetInactivity);
			this.resetInactivity();
			history.pushState({page: "stay"}, "", window.location.href);
			document.addEventListener("visibilitychange", function() {
				if (document.visibilityState === "hidden") {
					self.pixelioCartAbandonRecoveryMoreShow();
				}
			});
			const self = this;
			jQuery(window).on('popstate.pixelio', function (event) {
				const original = event.originalEvent;
				if (original && original.state && original.state.page === "stay") {
					self.trackPage(original.state.page);
				}
			});
			document.addEventListener("mouseleave", (e) => {
				if (e.clientY <= 0) {

						self.pixelioCartAbandonRecoveryMoreShow();

				}
			});
			jQuery(document).on('click', '.pixelio-cart-abandon-recovery-more-copy-coupon-btn', function (e) {
				e.preventDefault();
				const $couponEl = jQuery('.pixelio-cart-abandon-recovery-more-coupon-code-text').first();

				if (!$couponEl.length) {
					alert('Coupon code not found.');
					return;
				}

				const couponCode = jQuery.trim($couponEl.text());

				if (!couponCode) {
					alert('Coupon code is empty.');
					return;
				}

				const $temp =jQuery('<textarea>');
				jQuery('body').append($temp);
				$temp.val(couponCode).select();
				document.execCommand('copy');
				$temp.remove();

				// Feedback utilisateur
				alert('Coupon code copied: ' + couponCode);
				self.pixelioCartAbandonRecoveryMoreHidePopup();
			});


		}
	}

	pixelioCartAbandonRecoveryMoreInitTimer(startTimer) {
		this.timer = new PixelioCartAbandonRecoveryMoreTimer();
		if (startTimer) {
			this.timer.pixelioCartAbandonRecoveryMoreStartTimer(
				this.pixelioCartAbandonRecoveryMoreUpdateTimeInPopup.bind(this),
				this.pixelioCartAbandonRecoveryMoreHidePopup.bind(this),
				this.className
			);
		}
	}

	pixelioCartAbandonRecoveryMoreInitScrollPercent() {
		const self = this;
		jQuery(window).off('scroll.pixelioScrollPercent').on('scroll.pixelioScrollPercent', function () {
			const scrollPercent = window.scrollY / (document.body.scrollHeight - window.innerHeight);
			if (scrollPercent > 0.5) {
				if (!window.location.href.includes("admin") && self.className.includes("-recovery-more-preview-container-exit-popup")) {
					self.pixelioCartAbandonRecoveryMoreShow();

				}
			}
		});
	}

	pixelioCartAbandonRecoveryMoreInitFrontendCall(isFrontend) {
		if (isFrontend) {
			this.#pixelioCartAbandonRecoveryMorePopupClick();
		}
	}

	pixelioCartAbandonRecoveryMoreInitEvent() {
		const $button = jQuery(`${PIXELIOCARTABANDONRECOVERYMOREEDITCONTAINERCLASS} ${PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS}`);
		if ($button.length) {
			$button.on('click.pixelio mouseenter.pixelio', this.#pixelioCartAbandonRecoveryMoreEditButtonHover.bind(this));
		}
	}

	pixelioCartAbandonRecoveryMoreDisabledAllButton() {
		const $buttons = jQuery(".pixelio-cart-abandon-recovery-more-pop-up-container");
		if ($buttons.length) {
			$buttons.each(function () {
				jQuery(this).on('click.pixelioDisabled', function (e) {
					e.preventDefault();
				});
			});
		}
	}

	#pixelioCartAbandonRecoveryMoreInitElements() {
		this.colorProperty = "color";
		this.backgroundProperty = "background-color";
		this.borderProperty = "border-color";
		this.popupYesThanksButtonOnHover = "pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color";
		this.popupHeaderClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-header",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-header-background-color"
		};
		this.popupNoThanksButtonClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color"
		};
		this.popupHeaderTitleCTAClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-action",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color"
		};
		this.popupHeaderTitleClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-header-title",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color"
		};
		this.popupBodyTimerContentClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-timer-content",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color"
		};
		this.popupYesButtonClass = {
			className: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color",
			child2: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color",
		};

		this.copyButtonClass = {
			className: ".pixelio-cart-abandon-recovery-more-copy-coupon-btn",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color",
			child2: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color",
		};

		this.cartTimerDescriptionClass = {
			className: ".pixelio-cart-abandon-recovery-more-cart-timer-description",
			child2: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color",
		};

		this.popupNewsletterButtonClass = {
			className: ".pixelio-cart-abandon-recovery-more-newsletters-submit",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color",
			child2: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color"
		};

		this.cartTimer = {
			className: ".pixelio-cart-abandon-recovery-more-cart-timer",
			child: ".pixelio-cart-abandon-recovery-more-pop-up-header-background-color"
		};

		this.elements = [
			{
				all: this.popupHeaderTitleCTAClass.className,
				selectors: this.popupHeaderTitleCTAClass.child,
				property: this.colorProperty
			},
			{
				all: this.popupHeaderTitleClass.className,
				selectors: this.popupHeaderTitleClass.child,
				property: this.colorProperty
			},
			{
				all: this.popupNoThanksButtonClass.className,
				selectors: this.popupNoThanksButtonClass.child,
				property: this.colorProperty
			},
			{
				all: this.popupBodyTimerContentClass.className,
				selectors: this.popupBodyTimerContentClass.child,
				property: this.colorProperty
			},
			{
				all: this.popupHeaderClass.className,
				selectors: this.popupHeaderClass.child,
				property: this.backgroundProperty
			},
			{
				all: this.popupYesButtonClass.className,
				selectors: this.popupYesButtonClass.child,
				property: this.backgroundProperty
			},
			{
				all: this.popupYesButtonClass.className,
				selectors: this.popupYesButtonClass.child,
				property: this.borderProperty
			},
			{
				all: this.popupYesButtonClass.className,
				selectors: this.popupYesButtonClass.child2,
				property: this.colorProperty
			},
			{
				all: this.copyButtonClass.className,
				selectors: this.copyButtonClass.child,
				property: this.backgroundProperty
			},
			{
				all: this.cartTimer.className,
				selectors: this.cartTimer.child,
				property: this.backgroundProperty
			},

			{
				all: this.cartTimerDescriptionClass.className,
				selectors: this.cartTimerDescriptionClass.child,
				property: this.colorProperty
			},
			{all: this.copyButtonClass.className, selectors: this.copyButtonClass.child2, property: this.colorProperty},
			{
				all: this.popupNewsletterButtonClass.className,
				selectors: this.popupNewsletterButtonClass.child,
				property: this.backgroundProperty
			},
			{
				all: this.popupNewsletterButtonClass.className,
				selectors: this.popupNewsletterButtonClass.child,
				property: this.borderProperty
			},
			{
				all: this.popupNewsletterButtonClass.className,
				selectors: this.popupNewsletterButtonClass.child2,
				property: this.colorProperty
			}
		];
	}

	#pixelioCartAbandonRecoveryMoreCreateColorInit() {
		this.pixelioCartAbandonRecoveryMoreCreateColor([
			{
				className: '.pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-header',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-header",
				callback: this.pixelioCartAbandonRecoveryMoreBackgroundSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-header-background-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-header-title',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-header-title",
				callback: this.pixelioCartAbandonRecoveryMoreTextSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-header-title-action',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-action",
				callback: this.pixelioCartAbandonRecoveryMoreTextSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup',
				selectors: '.pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup',
				callback: this.pixelioCartAbandonRecoveryMoreTextSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-edit-container .pixelio-cart-abandon-recovery-more-pop-up-timer-content',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-timer-content",
				callback: this.pixelioCartAbandonRecoveryMoreTextSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-popup-yes-background',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup",
				callback: this.pixelioCartAbandonRecoveryMoreBackgroundSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-popup-yes-text',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup",
				callback: this.pixelioCartAbandonRecoveryMoreTextSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color"
			},
			{
				className: '.pixelio-cart-abandon-recovery-more-popup-yes-hover',
				selectors: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup",
				callback: this.pixelioCartAbandonRecoveryMoreHoverSave.bind(this),
				inputSelector: ".pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color"
			}
		]);
	}

	#pixelioCartAbandonRecoveryMorePopupClick() {
		const self = this;
		const $noThanks = jQuery(".pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup");
		if ($noThanks.length) {
			$noThanks.on('click.pixelioNoThanks', function () {
				self.#pixelioCartAbandonRecoveryMoreClose();
				localStorage.setItem("exit_popup" + self.className, 1);
			});
		}

		jQuery(PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS).off('click.pixelioThanks').on('click.pixelioThanks', function (e) {
			e.preventDefault();
			const data = new FormData();
			const $rgpdCheckbox = jQuery('input[type="checkbox"][name="pixelio-abandon-recovery-more-rgpd"], ' +
				'input[type="checkbox"][name="pixelio-cart-abandon-recovery-more-pop-up-rgpd"]'
			);

			const isRgpdActive = Boolean(Number(pixelio_ajax.isRgpdActive));

			const rgpdChecked = $rgpdCheckbox.length > 0
				? $rgpdCheckbox.is(':checked')
				: false;

			const rgpdDefinition = $rgpdCheckbox.length > 0
				? $rgpdCheckbox.closest('label').text().trim()
				: '';

			if (isRgpdActive && $rgpdCheckbox.length > 0 && !rgpdChecked) {
				alert(pixelio_ajax.missing_rgpd);
				return;
			}

			const email = jQuery('#pixelio-cart-abandon-recovery-more-email').val();
			if (!email) {
				return;
			}

			if (isRgpdActive && $rgpdCheckbox.length > 0) {
				data.append('pixelio-cart-abandon-recovery-more-pop-up-rgpd', rgpdChecked ? '1' : '0');
				data.append('pixelio-cart-abandon-recovery-more-pop-up-rgpd-label', rgpdDefinition);
			}
			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') || ''
			);

			data.append('pixelio-cart-abandon-recovery-more-pop-up-email', email);
			self.ajaxRequest.makeAjaxRequest("pixelio_cart_abandon_recovery_more_create_temporary_coupon", data, function (response) {
				if (response.success) {
					localStorage.setItem("exit_popup" + self.className, 1);
					localStorage.setItem("pixelio_email", 1);
					jQuery(".coupon-container").removeClass("pixelio-cart-abandon-recovery-more-hide-it");
					const couponContainer = jQuery('.pixelio-cart-abandon-recovery-more-hide-div-if-form-showed');
					couponContainer.removeClass('pixelio-cart-abandon-recovery-more-hide-it');
					jQuery(PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS).parent().addClass('pixelio-cart-abandon-recovery-more-hide-it');
					jQuery(".pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup").addClass('pixelio-cart-abandon-recovery-more-hide-it');
					jQuery(".pixelio-cart-abandon-recovery-more-coupon-code-text").text(response.data.coupon_code);
					if (self.positiveClickCallback != null) {
						self.positiveClickCallback();
					}
				} else {
					console.warn("Server returned an error:", response.data);
				}
			},	function (response) {
					alert(response.data.message);
			});
		});
	}

	pixelioCartAbandonRecoveryMoreBackgroundSave(element, color, selectors = null, inputSelector = null) {
		if (selectors != null) {
			const $els = jQuery(selectors);
			if ($els.length) {
				$els.each(function () {
					jQuery(this).css({
						'background-color': color,
						'border-color': color
					});
				});
			}
		} else if (element) {
			jQuery(element).css({
				'background-color': color,
				'border-color': color
			});
		}
		this.pixelioCartAbandonRecoveryMoreSetValue(inputSelector, color);
	}

	pixelioCartAbandonRecoveryMoreSetValue(inputSelector, color) {
		if (inputSelector != null) {
			const $inputs = jQuery(inputSelector);
			if ($inputs.length) {
				$inputs.each(function () {
					jQuery(this).val(color);
				});
			}
		}
	}

	pixelioCartAbandonRecoveryMoreHoverSave(element, color, selectors = null, inputSelector = null) {
		this.pixelioCartAbandonRecoveryMoreAddOrRemoveClass(selectors, element, false);
		this.#pixelioCartAbandonRecoveryMoreCreateDynamicStyle();
		this.pixelioCartAbandonRecoveryMoreAddOrRemoveClass(selectors, element, true);
		this.pixelioCartAbandonRecoveryMoreSetValue(inputSelector, color);
	}

	pixelioCartAbandonRecoveryMoreAddOrRemoveClass(selectors, element, add = true) {
		if (selectors != null && jQuery(selectors).length) {
			jQuery(selectors).each(function () {
				if (add) {
					jQuery(this).addClass(this.uniqueClass);
				} else {
					jQuery(this).removeClass(this.uniqueClass);
				}
			}.bind(this));
		} else if (element) {
			if (add) {
				jQuery(element).addClass(this.uniqueClass);
			} else {
				jQuery(element).removeClass(this.uniqueClass);
			}
		}
	}

	pixelioCartAbandonRecoveryMoreTextSave(element, color, selectors = null, inputSelector = null) {
		if (selectors != null) {
			const $els = jQuery(selectors);
			if ($els.length) {
				jQuery($els).each(function () {
					this.style.setProperty('color', color, 'important');
				});
			}
		} else if (element) {
			this.style.setProperty('color', color, 'important');
		}
		this.pixelioCartAbandonRecoveryMoreSetValue(inputSelector, color);
	}

	#pixelioCartAbandonRecoveryMoreEditButtonHover() {
		const yesButtonSelector = this.yesButtonSelector;
		const $yes = jQuery(this.yesButtonSelector);
		if ($yes.length) {
			$yes.removeClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		}

		const self = this;
		const intervalButtonState = setInterval(function () {
			clearInterval(intervalButtonState);
			const $yb = jQuery(yesButtonSelector);
			if ($yb.length) {
				$yb.addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
			}
		}, 10000);
	}

	pixelioCartAbandonRecoveryMoreShow() {
		const $popup = jQuery(this.className);
		if ($popup.length  &&
			((localStorage.getItem('exit_popup' + this.className) === null && localStorage.getItem("pixelio_email") == null) ||
				this.demo ||
				(((this.className.includes("preview") || this.className.includes("edit")) && window.location.href.includes("admin"))  )
			)
		) {
			$popup.removeClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		}

		if (this.startTimer) {
			this.timer.pixelioCartAbandonRecoveryMoreStartTimer(
				this.pixelioCartAbandonRecoveryMoreUpdateTimeInPopup.bind(this),
				this.pixelioCartAbandonRecoveryMoreHidePopup.bind(this),
				this.className
			);
		}

		if (this.onShowCallback) this.onShowCallback();
	}

	#pixelioCartAbandonRecoveryMoreCreateDynamicStyle() {
		this.uniqueClass = 'dynamic-hover-' + Math.floor(Math.random() * 10000);
		const hoverValue = jQuery(`.${this.popupYesThanksButtonOnHover}`).val() || '';
		const newHoverRule = `
			.${this.uniqueClass}:hover {
				background-color: ${hoverValue} !important;
				border-color: ${hoverValue} !important;
			}
		`;
		const $style = jQuery('<style type="text/css"></style>').text(newHoverRule);
		jQuery('head').append($style);
	}

	pixelioCartAbandonRecoveryMoreHidePopup() {
		jQuery(this.className).addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		localStorage.setItem('remaining' + this.className, -1);
		localStorage.setItem('exit_popup' + this.className, 1);
	}

	#pixelioCartAbandonRecoveryMoreDisableBackgroundOfPreview() {
		const self = this;
		const $disabledBackgrounds = jQuery('.pixelio-cart-abandon-recovery-more-disabled-background');
		if ($disabledBackgrounds.length) {
			$disabledBackgrounds.each(function () {
				jQuery(this).on('click.pixelioDisableBg', function (event) {
					event.preventDefault();
					jQuery(this).parent().addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
					if (self.onHideCallback) {
						self.onHideCallback();
					}
				});
			});
		}
	}

	pixelioCartAbandonRecoveryMoreUpdateTimeInPopup() {
		const $timerContent = jQuery(`${this.className} .pixelio-cart-abandon-recovery-more-pop-up-timer-content`);
		if ($timerContent.length) {
			this.timer.pixelioCartAbandonRecoveryMoreUpdateTimerContent($timerContent.toArray());
		}
	}

	#pixelioCartAbandonRecoveryMoreClose() {
		if (this.timer && typeof this.timer.pixelioCartAbandonRecoveryMoreClearTimer === 'function') {
			this.timer.pixelioCartAbandonRecoveryMoreClearTimer();
		}

		jQuery(this.className).each(function () {
			jQuery(this).addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		});

		if (window.location.href.includes("admin")) {
			const $saveBtn = jQuery("#pixelio-cart-abandon-recovery-more-save-button");
			if ($saveBtn.length) {
				$saveBtn.removeClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
			}
		}

		const $byId = jQuery(`${this.className}`);
		if ($byId.length) {
			$byId.addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		}
	}

	#pixelioCartAbandonRecoveryMoreCloseIconClick() {
		const self = this;
		jQuery(".pixelio-cart-abandon-recovery-more-pop-up-close").each(function () {

			jQuery(this).on("click.pixelio-cart-abandon-recovery-more-close", function () {
				localStorage.setItem('exit_popup' + self.className, 1);
				self.#pixelioCartAbandonRecoveryMoreClose();
				localStorage.setItem("consent_refused" + self.className, 0);
			});
		});
	}

	#pixelioCartAbandonRecoveryMoreSetPopUpProperty(selectors, property, e) {
		const $sel = jQuery(selectors);
		if ($sel.length && $sel.val() !== "" && e) {
			// jQuery sur DOM element sûr
			jQuery(e).css(property, $sel.val());
		}
	}

	#pixelioCartAbandonRecoveryMoreAddClassToElement(selectors, uniqueClass, e) {
		const $sel = jQuery(selectors);
		if ($sel.length && $sel.val() !== "") {
			this.#pixelioCartAbandonRecoveryMoreCreateDynamicStyle();
			jQuery(e).addClass(uniqueClass);
		}
	}

	#pixelioCartAbandonRecoveryMoreInitColor() {
		const self = this;
		this.elements.forEach(function (element) {
			const $all = jQuery(element.all);
			if ($all.length) {
				$all.each(function () {
					const e = this;
					if (element.all === self.popupNewsletterButtonClass.className || element.all === self.popupYesButtonClass.className) {
						self.#pixelioCartAbandonRecoveryMoreAddClassToElement(self.popupYesThanksButtonOnHover, self.uniqueClass, e);
					}
					self.#pixelioCartAbandonRecoveryMoreSetPopUpProperty(element.selectors, element.property, e);
				});
			}
		});
	}

}
