jQuery(function ($) {

	const checkbox = $('.pixelio-cart-abandon-recovery-more-opt-in-checkbox-with-name');

	checkbox.on('change', function () {
		change_label($(this));

		addButtonsClick($(this));
	});


	function change_label(input) {

		var wrapper = input.closest('.pixelio-cart-abandon-recovery-more-input-wrapper');
		if(wrapper.length===0) {
			wrapper = input.closest('.pixelio-cart-abandon-recovery-more-opt-in-checkbox-container');
		}

		const label_yes = wrapper.find('.pixelio-cart-abandon-recovery-more-checkbox-yes');
		const label_no  = wrapper.find('.pixelio-cart-abandon-recovery-more-checkbox-no');

		const onShow = input.data('onshow');

		const target = $('.pixelio-cart-abandon-recovery-more-opt-in-checkbox-' + onShow);
		const dependency = target.closest('.pixelio-cart-abandon-recovery-more-all-input-dependency');

		if (input.is(':checked')) {
			dependency.removeClass('pixelio-cart-abandon-recovery-more-hide-it');

			label_yes.removeClass('pixelio-cart-abandon-recovery-more-hide-it');
			label_no.addClass('pixelio-cart-abandon-recovery-more-hide-it');

		} else {

			dependency.addClass('pixelio-cart-abandon-recovery-more-hide-it');

			if (target.is(':checked')) {
				target
					.closest('.pixelio-cart-abandon-recovery-more-input-wrapper')
					.find('.pixelio-cart-abandon-recovery-more-slider-not-pro')
					.trigger('click');
			}

			label_yes.addClass('pixelio-cart-abandon-recovery-more-hide-it');
			label_no.removeClass('pixelio-cart-abandon-recovery-more-hide-it');
		}
	}
	var pixelioPopup;
	var boughtPopup;

	const previewBtnClass = '.pixelio-cart-abandon-recovery-more-input-button-setting-action-preview';
	const editBtnClass    = '.pixelio-cart-abandon-recovery-more-input-button-setting-action-edit';
	const boughtTarget    = 'pixelio-cart-abandon-recovery-more-show-others-buy-checkbox';

	const buttonClasses = [previewBtnClass, editBtnClass];

	function addButtonsClick(input) {

		$.each(buttonClasses, function (index, className) {

			const buttons = input.closest('.pixelio-cart-abandon-recovery-more-input-wrapper').find(className);

			if (buttons.length > 0) {

				buttons.each(function () {

					$(this).off('click.pixelio').on('click.pixelio', function (e) {
						e.preventDefault();

						let addClass = (className === editBtnClass) ? "-edit" : "-preview";

						const clicked_on = $(this).data("clicked_on");
						const fullClass = "." + clicked_on + addClass;

						if (clicked_on === boughtTarget) {

							boughtPopup = new PixelioCartAbandonRecoveryMoreBoughtPopup(
								fullClass,
								hideSaveButton,
								showSaveButton,
								false
							);
							boughtPopup.pixelioCartAbandonRecoveryMoreShow();

						} else {

							pixelioPopup = new PixelioCartAbandonRecoveryMorePopUp(
								fullClass,
								hideSaveButton,
								showSaveButton,
								false,
								null,
								false
							);

							pixelioPopup.pixelioCartAbandonRecoveryMoreDisabledAllButton();
							pixelioPopup.pixelioCartAbandonRecoveryMoreShow();
						}

					});

				});

			}

		});

	}

});
