export class ModalManager {
    constructor() {
        this.initPasswordToggles();
        this.initModals();
        this.initConfirmations();
    }

    initPasswordToggles() {
        document.querySelectorAll(".auth-toggle-pw").forEach((btn) => {
            btn.addEventListener("click", () => {
                const input = btn.parentElement.querySelector("input");
                if (!input) {
                    return;
                }
                const type = input.type === "password" ? "text" : "password";
                input.type = type;
                btn.textContent = type === "password" ? "\u{1F441}" : "\u{1F441}";
            });
        });
    }

    initModals() {
        document.querySelectorAll("[data-modal]").forEach((trigger) => {
            trigger.addEventListener("click", (e) => {
                e.preventDefault();
                const modalId = trigger.dataset.modal;
                const modal = document.getElementById(modalId);
                if (modal) {
                    modal.classList.add("visible");
                }
            });
        });

        document.querySelectorAll(".modal-overlay").forEach((modal) => {
            modal.addEventListener("click", (e) => {
                if (e.target === modal) {
                    modal.classList.remove("visible");
                }
            });
            modal.querySelectorAll(".modal-close").forEach((closeBtn) => {
                closeBtn.addEventListener("click", () => {
                    modal.classList.remove("visible");
                });
            });
        });
    }

    initConfirmations() {
        document.querySelectorAll("[data-confirm]").forEach((btn) => {
            btn.addEventListener("click", (e) => {
                if (!confirm(btn.dataset.confirm)) {
                    e.preventDefault();
                }
            });
        });
    }
}
