/**
 * Toggle ver/ocultar + indicador informativo de fortaleza (registro / reset).
 */
(function () {
    'use strict';

    function isStrongPassword(pw) {
        if (!pw || pw.length < 8) {
            return false;
        }
        var lower = /[a-z]/.test(pw);
        var upper = /[A-Z]/.test(pw);
        var digit = /[0-9]/.test(pw);
        var special = /[^A-Za-z0-9]/.test(pw);
        var sets = [lower, upper, digit, special].filter(Boolean).length;
        if (pw.length >= 14 && sets >= 2) {
            return true;
        }
        if (pw.length >= 8 && sets >= 3) {
            return true;
        }
        if (pw.length >= 10 && sets >= 4) {
            return true;
        }
        return false;
    }

    function labelForLang(strong) {
        return strong
            ? window.rtPasswordI18n && window.rtPasswordI18n.secure
                ? window.rtPasswordI18n.secure
                : 'Contraseña segura'
            : window.rtPasswordI18n && window.rtPasswordI18n.unsafe
              ? window.rtPasswordI18n.unsafe
              : 'Contraseña no segura';
    }

    function bindField(root) {
        var input = root.querySelector('.rt-password-field__input');
        var toggle = root.querySelector('[data-rt-password-toggle]');
        var meterBlock = root.querySelector('[data-rt-password-meter-block]');
        var meterLabel = root.querySelector('[data-rt-password-meter-label]');
        var useMeter = root.getAttribute('data-rt-password-meter') === '1';

        if (!input || !toggle) {
            return;
        }

        toggle.addEventListener('click', function () {
            var show = input.getAttribute('type') === 'password';
            input.setAttribute('type', show ? 'text' : 'password');
            toggle.setAttribute('aria-pressed', show ? 'true' : 'false');
            toggle.setAttribute(
                'aria-label',
                show
                    ? window.rtPasswordI18n && window.rtPasswordI18n.hide
                        ? window.rtPasswordI18n.hide
                        : 'Ocultar contraseña'
                    : window.rtPasswordI18n && window.rtPasswordI18n.show
                      ? window.rtPasswordI18n.show
                      : 'Mostrar contraseña'
            );
            var iconShow = toggle.querySelector('.rt-password-toggle__icon--show');
            var iconHide = toggle.querySelector('.rt-password-toggle__icon--hide');
            if (iconShow && iconHide) {
                iconShow.classList.toggle('d-none', show);
                iconHide.classList.toggle('d-none', !show);
            }
        });

        if (!useMeter || !meterBlock || !meterLabel) {
            return;
        }

        function updateMeter() {
            var v = input.value;
            if (!v.length) {
                meterBlock.hidden = true;
                root.classList.remove('rt-password-field--unsafe', 'rt-password-field--safe');
                return;
            }
            meterBlock.hidden = false;
            var strong = isStrongPassword(v);
            meterLabel.textContent = labelForLang(strong);
            root.classList.toggle('rt-password-field--unsafe', !strong);
            root.classList.toggle('rt-password-field--safe', strong);
        }

        input.addEventListener('input', updateMeter);
        input.addEventListener('change', updateMeter);
        updateMeter();
    }

    document.addEventListener('DOMContentLoaded', function () {
        document.querySelectorAll('[data-rt-password-field]').forEach(bindField);
    });
})();
