let suppressQuantityReportValidityOnBlur = false;

$(document)
    .off('blur.quantityValidation', '#quantity-field')
    .on('blur.quantityValidation', '#quantity-field', function () {
        if (suppressQuantityReportValidityOnBlur) {
            suppressQuantityReportValidityOnBlur = false;
            return;
        }

        this.reportValidity();
    });

$(document)
    .off('keydown.quantityValidation', '#quantity-field')
    .on('keydown.quantityValidation', '#quantity-field', function (e) {
        if (e.key !== 'Enter') return;

        e.preventDefault();
        e.stopPropagation();

        const isValid = this.checkValidity();

        Wick.FEMock.dispatchProductAddedToCart(Wick.FEMock.getCartPdpHtml({ hasError: !isValid }));

        suppressQuantityReportValidityOnBlur = true;
        this.blur();

        setTimeout(() => {
            const $focusTarget = $('.custom-slider.just-added .close-popup').first();
            if ($focusTarget.length) $focusTarget.trigger('focus');
        }, 150);
    });
