<script>

    const getUrl = window.location;
    const baseUri = "{$baseUri}";
    const baseUrl = getUrl.protocol + "//" + "{$domain}" + baseUri;

    const _XT = {
        baseUrl: baseUrl,
        baseUri: baseUri,
        language:  "{$language}",
        page : {
            page_name : "{$page_name}"
        },
        version : {
            type: "{$version_type}",
            version : "{$version}"
        },
        passwordMinLength: {$smarty.const._STORE_PASSWORD_MIN_LENGTH},
        passwordMaxLength: {$smarty.const._STORE_PASSWORD_MAX_LENGTH},
        passwordSpecialChars: "{$REGEX_PASSWORD_SPECIAL_CHARS}",

        messages: {
            password:  {
                minLength: "{$smarty.const.TEXT_ERROR_PASSWORD_MIN_LENGTH}, min {$smarty.const._STORE_PASSWORD_MIN_LENGTH}",
                maxLength: "{$smarty.const.TEXT_ERROR_PASSWORD_MAX_LENGTH}, max {$smarty.const._STORE_PASSWORD_MAX_LENGTH}",
                requireUppercase: "{$smarty.const.TEXT_ERROR_PASSWORD_REQUIRE_UPPERCASE}",
                requireLowercase: "{$smarty.const.TEXT_ERROR_PASSWORD_REQUIRE_LOWERCASE}",
                requireNumber: "{$smarty.const.TEXT_ERROR_PASSWORD_REQUIRE_NUMBER}",
                requireSpecialChar: "{$smarty.const.TEXT_ERROR_PASSWORD_REQUIRE_SPECIAL_CHAR}",
                strengthWeak: "{$smarty.const.TEXT_PASSWORD_STRENGTH_WEAK}",
                strengthMedium: "{$smarty.const.TEXT_PASSWORD_STRENGTH_MEDIUM}",
                strengthStrong: "{$smarty.const.TEXT_PASSWORD_STRENGTH_STRONG}"
            }
        }
    };

    Object.freeze(_XT);
    window.XT = _XT;

    function validatePassword(password, minLength, maxLength) {
        const errors = [];
        let score = 0;

        if (password.length >= minLength) score++;
        else errors.push(XT.messages.password.minLength);

        if (/[A-Z]/.test(password)) score++;
        else errors.push(XT.messages.password.requireUppercase);

        if (/[a-z]/.test(password)) score++;
        else errors.push(XT.messages.password.requireLowercase);

        if (/\d/.test(password)) score++;
        else errors.push(XT.messages.password.requireNumber);

        if (/[?§=!@#$%&*()\-_]/.test(password)) score++;
        else errors.push(XT.messages.password.requireSpecialChar + " " + window.XT.passwordSpecialChars);

        if (password.length > maxLength) {
            score = 0;
            errors.push(XT.messages.password.maxLength);
        }

        return { errors, score };
    }

    function updatePasswordUI() {
        const password = passwordInput?.value || "";
        const { errors, score } = validatePassword(password, window.XT.passwordMinLength, window.XT.passwordMaxLength);

        // Regeln / Fehler
        if(typeof strengthDiv !== 'undefined')
        {
            rulesList.innerHTML = "";
            errors.forEach(error => {
                const li = document.createElement("li");
                li.textContent = error;
                rulesList.appendChild(li);
            });
        }

        // aria-invalid setzen
        passwordInput.setAttribute(
            "aria-invalid",
            errors.length > 0 ? "true" : "false"
        );

        if(typeof strengthDiv !== 'undefined')
        {
            if (password.length === 0) {
                strengthDiv.textContent = "";
            } else if (score <= 2) {
                strengthDiv.textContent = XT.messages.password.strengthWeak;
            } else if (score <= 4) {
                strengthDiv.textContent = XT.messages.password.strengthMedium;
            } else {
                strengthDiv.textContent = XT.messages.password.strengthStrong;
            }
        }

        // Balken (0–5 → %)
        if(typeof strengthFill !== 'undefined')
        {
            const percentage = (score / 5) * 100;
            strengthFill.style.width = `${literal}{percentage}{/literal}%`;

            // Farbe setzen (nicht alleinige Info!)
            if (score <= 2) {
                strengthFill.style.backgroundColor = "#d32f2f"; // rot
            } else if (score <= 4) {
                strengthFill.style.backgroundColor = "#f9a825"; // orange
            } else {
                strengthFill.style.backgroundColor = "#388e3c"; // grün
            }
        }

        // ARIA-Progressbar aktualisieren
        if(typeof strengthBar !== 'undefined')
        {
            strengthBar.setAttribute("aria-valuenow", score.toString());
        }
    }

</script>