(function () {
    "use strict";

    function computeStrength(password) {
        let score = 0;
        if (password.length >= 8) score++;
        if (/[A-Z]/.test(password)) score++;
        if (/[0-9]/.test(password)) score++;
        if (/[^A-Za-z0-9]/.test(password)) score++;
        return score;
    }

    const LEVELS = [
        { label: "", width: "0%", color: "" },
        { label: "Weak", width: "25%", color: "bg-danger" },
        { label: "Fair", width: "50%", color: "bg-warning" },
        { label: "Good", width: "75%", color: "bg-info" },
        { label: "Strong", width: "100%", color: "bg-success" },
    ];

    function initPasswordStrength() {
        const input = document.getElementById("reg_password");
        const bar = document.getElementById("password_strength_bar");
        const text = document.getElementById("password_strength_text");
        if (!input || !bar || !text) return;

        input.addEventListener("input", function () {
            const score = computeStrength(this.value);
            const level = LEVELS[score];
            bar.style.width = level.width;
            bar.className = "progress-bar " + level.color;
            text.textContent = level.label;
        });
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", initPasswordStrength);
    } else {
        initPasswordStrength();
    }
})();
