(function () {
    let discountPopupTimer = null;

    function getDiscountPopup() {
        return document.getElementById('discountPopup');
    }

    function openDiscountPopup() {
        const popup = getDiscountPopup();

        if (!popup) {
            return;
        }

        if (localStorage.getItem('loveu_discount_popup_closed') === '1') {
            popup.classList.remove('show');
            popup.classList.add('is-hidden');
            return;
        }

        popup.classList.remove('is-hidden');

        requestAnimationFrame(function () {
            popup.classList.add('show');
        });
    }

    function closeDiscountPopup() {
        const popup = getDiscountPopup();

        if (!popup) {
            return;
        }

        if (discountPopupTimer) {
            clearTimeout(discountPopupTimer);
            discountPopupTimer = null;
        }

        localStorage.setItem('loveu_discount_popup_closed', '1');

        popup.classList.remove('show');
        popup.classList.add('is-hidden');

        popup.style.display = 'none';
    }

    window.closeDiscountPopup = closeDiscountPopup;

    document.addEventListener('DOMContentLoaded', function () {
        const popup = getDiscountPopup();

        if (popup) {
            popup.classList.add('is-hidden');
            popup.classList.remove('show');

            if (localStorage.getItem('loveu_discount_popup_closed') !== '1') {
                discountPopupTimer = setTimeout(openDiscountPopup, 2500);
            }

            document.addEventListener('click', function (event) {
                const closeButton = event.target.closest('.discount-popup-close, .discount-popup-skip');

                if (closeButton) {
                    event.preventDefault();
                    closeDiscountPopup();
                    return;
                }

                if (event.target === popup) {
                    closeDiscountPopup();
                }
            });

            document.addEventListener('keydown', function (event) {
                if (event.key === 'Escape') {
                    closeDiscountPopup();
                }
            });
        }

        const toast = document.getElementById('checkoutToast');

        if (toast) {
            setTimeout(function () {
                closeCheckoutToast();
            }, 4500);
        }

        const passwordToggleButtons = document.querySelectorAll('.lue-password-toggle');

        passwordToggleButtons.forEach(function (button) {
            button.addEventListener('click', function () {
                const targetId = button.getAttribute('data-target');
                const input = document.getElementById(targetId);

                if (!input) {
                    return;
                }

                const isPassword = input.getAttribute('type') === 'password';

                input.setAttribute('type', isPassword ? 'text' : 'password');
                button.classList.toggle('is-visible', isPassword);
                button.setAttribute('aria-label', isPassword ? 'Hide password' : 'Show password');
            });
        });

        const quantityControls = document.querySelectorAll('.lue-qty-control');

        quantityControls.forEach(function (control) {
            const input = control.querySelector('.lue-qty-input');
            const decreaseButton = control.querySelector('[data-qty-action="decrease"]');
            const increaseButton = control.querySelector('[data-qty-action="increase"]');

            if (!input || !decreaseButton || !increaseButton) {
                return;
            }

            function syncButtons() {
                const value = parseInt(input.value || '1', 10);
                const min = parseInt(input.getAttribute('min') || '1', 10);
                const max = parseInt(input.getAttribute('max') || '999', 10);

                decreaseButton.disabled = value <= min;
                increaseButton.disabled = value >= max;
            }

            decreaseButton.addEventListener('click', function () {
                const min = parseInt(input.getAttribute('min') || '1', 10);
                const currentValue = parseInt(input.value || '1', 10);

                input.value = Math.max(min, currentValue - 1);
                syncButtons();
            });

            increaseButton.addEventListener('click', function () {
                const max = parseInt(input.getAttribute('max') || '999', 10);
                const currentValue = parseInt(input.value || '1', 10);

                input.value = Math.min(max, currentValue + 1);
                syncButtons();
            });

            input.addEventListener('input', function () {
                const min = parseInt(input.getAttribute('min') || '1', 10);
                const max = parseInt(input.getAttribute('max') || '999', 10);
                let value = parseInt(input.value || '1', 10);

                if (Number.isNaN(value)) {
                    value = min;
                }

                input.value = Math.min(max, Math.max(min, value));
                syncButtons();
            });

            syncButtons();
        });

        const sameAddressCheckbox = document.getElementById('sameAsBillingAddress');

if (sameAddressCheckbox) {
    const addressFields = [
        ['billingAddress1', 'shippingAddress1'],
        ['billingAddress2', 'shippingAddress2'],
        ['billingCity', 'shippingCity'],
        ['billingRegion', 'shippingRegion'],
        ['billingPostcode', 'shippingPostcode'],
        ['billingCountry', 'shippingCountrySelect']
    ];

    function copyBillingToShipping() {
        addressFields.forEach(function (pair) {
            const billingField = document.getElementById(pair[0]);
            const shippingField = document.getElementById(pair[1]);

            if (!billingField || !shippingField) {
                return;
            }

            shippingField.value = billingField.value;
            shippingField.dispatchEvent(new Event('change', { bubbles: true }));
        });
    }

    function toggleShippingLock() {
        const shouldCopy = sameAddressCheckbox.checked;

        if (shouldCopy) {
            copyBillingToShipping();
        }

        addressFields.forEach(function (pair) {
            const shippingField = document.getElementById(pair[1]);

            if (!shippingField) {
                return;
            }

            shippingField.readOnly = shouldCopy && shippingField.tagName !== 'SELECT';
            shippingField.classList.toggle('is-copied-address', shouldCopy);

            if (shippingField.tagName === 'SELECT') {
                shippingField.classList.toggle('is-copied-address', shouldCopy);
            }
        });
    }

    sameAddressCheckbox.addEventListener('change', toggleShippingLock);

    addressFields.forEach(function (pair) {
        const billingField = document.getElementById(pair[0]);

        if (!billingField) {
            return;
        }

        billingField.addEventListener('input', function () {
            if (sameAddressCheckbox.checked) {
                copyBillingToShipping();
            }
        });

        billingField.addEventListener('change', function () {
            if (sameAddressCheckbox.checked) {
                copyBillingToShipping();
            }
        });
    });

    toggleShippingLock();
}
    });

    function closeCheckoutToast() {
        const toast = document.getElementById('checkoutToast');

        if (!toast) {
            return;
        }

        toast.classList.add('hide');

        setTimeout(function () {
            toast.remove();
        }, 260);
    }

    window.closeCheckoutToast = closeCheckoutToast;
})();

function copyReferralLink() {
    const input = document.getElementById('referralLinkInput');
    const message = document.getElementById('referralCopyMessage');

    if (!input) {
        return;
    }

    input.select();
    input.setSelectionRange(0, 99999);

    navigator.clipboard.writeText(input.value).then(function () {
        if (message) {
            message.textContent = 'Referral link copied.';
            message.classList.add('show');
        }
    }).catch(function () {
        document.execCommand('copy');

        if (message) {
            message.textContent = 'Referral link copied.';
            message.classList.add('show');
        }
    });
}

window.copyReferralLink = copyReferralLink;