document.addEventListener('DOMContentLoaded', function () {
    var strings = window.griffinformsPasswordStrings || {
        showLabel: 'Show',
        hideLabel: 'Hide',
        loading: 'Decrypting...',
        error: 'Unable to reveal password.'
    };

    document.addEventListener('click', function (event) {
        var target = event.target;
        if (!target.classList.contains('griffinforms-password-reveal')) {
            return;
        }

        event.preventDefault();

        var container = target.closest('.griffinforms-submission-password');
        if (!container) {
            return;
        }

        var statusEl = container.querySelector('.griffinforms-password-status');
        var plainEl = container.querySelector('.griffinforms-password-plain');
        var maskEl = container.querySelector('.griffinforms-password-mask');

        if (!plainEl || !maskEl) {
            return;
        }

        var isEncrypted = String(container.getAttribute('data-gf-encrypted')) === '1';
        var isLoaded = container.getAttribute('data-gf-loaded') === '1';
        var maskPlaceholder = container.getAttribute('data-gf-mask');
        if (!maskPlaceholder) {
            var submissionData = window.griffinformsSubmissionData;
            if (submissionData && submissionData.password && submissionData.password.mask) {
                maskPlaceholder = submissionData.password.mask;
            } else {
                maskPlaceholder = '******';
            }
        }

        if (!maskEl.value) {
            maskEl.value = maskPlaceholder;
        }

        if (isLoaded) {
            var isVisible = !plainEl.classList.contains('visually-hidden');
            if (isVisible) {
                plainEl.classList.add('visually-hidden');
                plainEl.classList.add('d-none');
                maskEl.value = maskPlaceholder;
                maskEl.classList.remove('d-none');
                maskEl.classList.remove('visually-hidden');
                target.textContent = strings.showLabel;
                if (statusEl) {
                    statusEl.textContent = '';
                }
            } else {
                plainEl.classList.remove('visually-hidden');
                plainEl.classList.remove('d-none');
                maskEl.classList.add('visually-hidden');
                maskEl.classList.add('d-none');
                target.textContent = strings.hideLabel;
                if (statusEl) {
                    if (container.getAttribute('data-gf-empty') === '1') {
                        statusEl.textContent = strings.empty || '';
                    } else {
                        statusEl.textContent = '';
                    }
                }
            }
            return;
        }

        if (!isEncrypted) {
            var plainValue = container.getAttribute('data-gf-plain') || '';
            plainEl.value = plainValue;
            plainEl.classList.remove('visually-hidden');
            plainEl.classList.remove('d-none');
            maskEl.classList.add('visually-hidden');
            maskEl.classList.add('d-none');
            maskEl.value = maskPlaceholder;
            target.textContent = strings.hideLabel;
            container.setAttribute('data-gf-loaded', '1');
            if (plainValue === '') {
                container.setAttribute('data-gf-empty', '1');
                if (statusEl) {
                    statusEl.textContent = strings.empty || '';
                }
            } else {
                container.removeAttribute('data-gf-empty');
                if (statusEl) {
                    statusEl.textContent = '';
                }
            }
            return;
        }

        var requestPassword = window.griffinformsRequestPassword;
        if (typeof requestPassword !== 'function') {
            if (statusEl) {
                statusEl.textContent = strings.error;
            }
            target.disabled = false;
            return;
        }

        if (statusEl) {
            statusEl.textContent = strings.loading;
        }
        target.disabled = true;

        requestPassword({
            nonce: container.getAttribute('data-gf-nonce'),
            fieldId: container.getAttribute('data-gf-field'),
            submissionId: container.getAttribute('data-gf-submission'),
            iteration: container.getAttribute('data-gf-iteration')
        }).then(function (decryptedValue) {
            target.disabled = false;
            plainEl.value = decryptedValue;
            plainEl.classList.remove('visually-hidden');
            plainEl.classList.remove('d-none');
            maskEl.classList.add('visually-hidden');
            maskEl.classList.add('d-none');
            maskEl.value = maskPlaceholder;
            if (statusEl) {
                if ((decryptedValue === '' || decryptedValue === null) && container.getAttribute('data-gf-empty') !== '1') {
                    statusEl.textContent = strings.empty || '';
                } else {
                    statusEl.textContent = '';
                }
            }
            container.setAttribute('data-gf-loaded', '1');
            container.setAttribute('data-gf-encrypted', '0');
            container.setAttribute('data-gf-plain', decryptedValue);
            if (decryptedValue === '') {
                container.setAttribute('data-gf-empty', '1');
            } else {
                container.removeAttribute('data-gf-empty');
            }
            target.textContent = strings.hideLabel;
        }).catch(function (errorMessage) {
            target.disabled = false;
            if (statusEl) {
                statusEl.textContent = errorMessage || strings.error;
            }
        });
    });
});
