import { PIXELIOCARTABANDONRECOVERYMOREHIDEIT } from "../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable";

export class PixelioCartAbandonRecoveryMoreSlider {

	constructor() {

		this.moveDependancyInCorrectParent();
		const self = this;
		jQuery('.pixelio-cart-abandon-recovery-more-slider').each(function () {
			self.#applySliderState(jQuery(this),true);
		});


		jQuery('input[type="checkbox"]').on('change', function () {
			const $clicked = jQuery(this);
			self.#applySliderState($clicked,false);
		});
	}

	moveDependancyInCorrectParent() {
		jQuery('[class*="-parent"]').each(function () {
			const $el = jQuery(this);

			// Get all classes
			const classes = $el.attr('class').split(/\s+/);

			// Find the "-parent" class
			classes.forEach(function (cls) {
				if (cls.includes('-parent')) {
					// Compute the target class by removing "-parent"
					const targetClass = cls.replace('-parent', '');

					// Remove the "-parent" class from the element
					$el.removeClass(cls);

					// Find the target div by class and append the current element
					const $target = jQuery('.' + targetClass).first();
					if ($target.length) {
						$el.appendTo($target);
					}
				}
			});
		});
	}

	#applySliderState($clicked, $init) {
		const isActive = this.#updateInputValueAndToggleSentence($clicked, $init);

		const onShow = $clicked
			.parent()
			.find('.pixelio-cart-abandon-recovery-more-opt-in-checkbox')
			.data('onshow');

		if (onShow) {
			// Split si plusieurs classes/ids séparés par des virgules
			const selectors = onShow.split(',').map(s => s.trim());

			selectors.forEach(selector => {
				// Tenter par classe
				let $onShowTarget = jQuery('.' + selector);

				// Si pas trouvé, tenter par name
				if ($onShowTarget.length === 0) {
					$onShowTarget = jQuery('input[name="' + selector + '"]');
				}

				if (isActive) {
					$onShowTarget.removeClass('pixelio-cart-abandon-recovery-more-hide-it');
				}else{
					$onShowTarget.addClass('pixelio-cart-abandon-recovery-more-hide-it');

				}

				// ✅ Ne prendre que les enfants directs
				const $dependencyWrapper = $onShowTarget.children('.pixelio-cart-abandon-recovery-more-all-input-dependency');

				if ($dependencyWrapper.length) {
					$dependencyWrapper.toggleClass(
						'pixelio-cart-abandon-recovery-more-hide-it',
						!isActive
					);

					if (!isActive) {
						// Reset uniquement les inputs checkbox / radio au premier niveau
						$dependencyWrapper.children('input[type="checkbox"], input[type="radio"], select').each(function () {
							if (this.type === 'checkbox' || this.type === 'radio') {
								this.checked = false;
								jQuery(this).trigger('change');
							}
						});
					}
				} else {
					console.warn('❌ onshow dependency introuvable :', selector);
				}
			});
		}
	}





	#updateInputValueAndToggleSentence($clicked,$init) {
		let $prev = $clicked.prev();
		let isActive = false;

		while ($prev.length) {
			if ($prev.is('input[type="hidden"]')) {

				const inputName = $prev.attr('name');
				const $yes = jQuery('#' + inputName + '-yes');
				const $no  = jQuery('#' + inputName + '-no');

				if ($prev.val() === 'on') {
					isActive = false;
					if(!$init){
						$prev.val('off');
						$yes.addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
						$no.removeClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
					}else{
						isActive = true;
					}

				} else {
					isActive = true;
					if(!$init){
						$prev.val('on');
						$yes.removeClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
						$no.addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
					}else{
						isActive = false;
					}

				}

				break;
			}
			$prev = $prev.prev();
		}

		return isActive;
	}
}
