import {PixelioCartAbandonRecoveryMoreSlider} from "../components/pixelio_cart_abandon_recovery_more_slider";
import {
	PIXELIOCARTABANDONRECOVERYMOREHIDEIT
} from "../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable";
import {PixelioCartAbandonRecoveryMoreBoughtPopup} from "../components/pixelio_cart_abandon_recovery_more_bought_notification_on_product";
import {PixelioCartAbandonRecoveryMorePopUp} from "../components/pixelio_cart_abandon_recovery_more_exit_intent_popup";
import "../components/pixelio_cart_abandon_recovery_more_jquery_checkbox_of_slider";
import {
	PixelioCartAbandonRecoveryMoreUpdate
} from "../components/pixelio_cart_abandon_recovery_more_update_popup";
jQuery(document).ready(function ($) {

	var pixelioCartAbandonRecoveryMorePopup;
	var pixelioCartAbandonRecoveryMoreSlider = new PixelioCartAbandonRecoveryMoreSlider();
	var pixelioCartAbandonRecoveryMoreButtonPreviewClass = '.pixelio-cart-abandon-recovery-more-input-button-setting-action-preview';
	var pixelioCartAbandonRecoveryMoreBoughtClickedOn = 'pixelio-cart-abandon-recovery-more-show-others-buy-checkbox';
	var pixelioCartAbandonRecoveryMoreButtonEditClass = '.pixelio-cart-abandon-recovery-more-input-button-setting-action-edit';
	var pixelioCartAbandonRecoveryMoreButtons = [pixelioCartAbandonRecoveryMoreButtonPreviewClass, pixelioCartAbandonRecoveryMoreButtonEditClass];
	var pixelioCartAbandonRecoveryMoreSaveButtonId = 'pixelio-cart-abandon-recovery-more-save-button';
	var pixelioCartAbandonRecoveryMoreSaveFormClass = 'pixelio-cart-abandon-recovery-more-general-settings-form';
	addButtonsClick();

	function addButtonsClick() {
		pixelioCartAbandonRecoveryMoreButtons.forEach(function (className) {
			const buttons = document.querySelectorAll(className);
			buttons.forEach((editButton) => {
				editButton.addEventListener('click', function (event) {
					event.preventDefault();
					var addClass = "-preview";
					if (className === pixelioCartAbandonRecoveryMoreButtonEditClass) {
						addClass = "-edit"
					}
					if ($(this).data("clicked_on") === pixelioCartAbandonRecoveryMoreBoughtClickedOn) {
						var pixelioCartAbandonRecoveryMoreBoughtPopup = new PixelioCartAbandonRecoveryMoreBoughtPopup("." + $(this).data("clicked_on") + addClass, hideSaveButton, showSaveButton, false);
						pixelioCartAbandonRecoveryMoreBoughtPopup.pixelioCartAbandonRecoveryMoreShow();
					} else {
						pixelioCartAbandonRecoveryMorePopup = new PixelioCartAbandonRecoveryMorePopUp("." + $(this).data("clicked_on") + addClass, hideSaveButton, showSaveButton, false, null, false);
						pixelioCartAbandonRecoveryMorePopup.pixelioCartAbandonRecoveryMoreDisabledAllButton();
						pixelioCartAbandonRecoveryMorePopup.pixelioCartAbandonRecoveryMoreShow();
					}

				});
			});
		});
	}

	document.getElementById(pixelioCartAbandonRecoveryMoreSaveButtonId).addEventListener('click', function () {
		const form = document.getElementById(pixelioCartAbandonRecoveryMoreSaveFormClass);
		form.submit();
	});


	function hideSaveButton() {
		document.getElementById(pixelioCartAbandonRecoveryMoreSaveButtonId).classList.add(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
	}

	function showSaveButton() {
		document.getElementById(pixelioCartAbandonRecoveryMoreSaveButtonId).classList.remove(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
	}


	function pixelioEnhanceSettingsUI() {
		const iconMap = [
			{ keywords: ['abandoned', 'cart', 'panier'], className: 'pixelio-settings-icon-cart' },
			{ keywords: ['email', 'mail', 'notification'], className: 'pixelio-settings-icon-mail' },
			{ keywords: ['gdpr', 'consent'], className: 'pixelio-settings-icon-gdpr' },
			{ keywords: ['map'], className: 'pixelio-settings-icon-map' },
			{ keywords: ['exclude', 'user'], className: 'pixelio-settings-icon-user' },
			{ keywords: ['popup', 'pop-up'], className: 'pixelio-settings-icon-popup' },
			{ keywords: ['discount', 'promo'], className: 'pixelio-settings-icon-discount' },
			{ keywords: ['delete', 'uninstall'], className: 'pixelio-settings-icon-trash' },
		];

		const $rows = $('.pixelio-cart-abandon-recovery-more-checkbox-container, .pixelio-cart-abandon-recovery-more-input-container, .pixelio-cart-abandon-recovery-more-radio-container');

		$rows.each(function () {
			const $row = $(this);
			const text = $row.text().toLowerCase();

			iconMap.forEach(function (item) {
				if (item.keywords.some(function (keyword) { return text.includes(keyword); })) {
					$row.addClass(item.className);
				}
			});
		});
	}

	pixelioEnhanceSettingsUI();

});

