document.addEventListener("DOMContentLoaded", () => {
    const menuButton = document.getElementById("mobile-menu-btn");
    const mainNav = document.getElementById("main-nav");

    const closeMenu = () => {
        if (!menuButton || !mainNav) {
            return;
        }

        menuButton.classList.remove("active");
        mainNav.classList.remove("active");
        menuButton.setAttribute("aria-expanded", "false");
        menuButton.setAttribute("aria-label", "Apri il menu");
    };

    if (menuButton && mainNav) {
        menuButton.addEventListener("click", () => {
            const isOpen = menuButton.getAttribute("aria-expanded") === "true";

            menuButton.classList.toggle("active", !isOpen);
            mainNav.classList.toggle("active", !isOpen);
            menuButton.setAttribute("aria-expanded", String(!isOpen));
            menuButton.setAttribute("aria-label", isOpen ? "Apri il menu" : "Chiudi il menu");
        });

        mainNav.querySelectorAll("a").forEach(link => {
            link.addEventListener("click", closeMenu);
        });

        document.addEventListener("keydown", event => {
            if (event.key === "Escape" && menuButton.getAttribute("aria-expanded") === "true") {
                closeMenu();
                menuButton.focus();
            }
        });
    }

    const params = new URLSearchParams(window.location.search);
    const dataLayer = window.dataLayer = window.dataLayer || [];

    document.querySelectorAll(".typeform").forEach(container => {
        const formId = container.dataset.formId;
        const fallback = container.parentElement?.querySelector(".typeform-fallback");

        if (!formId) {
            console.warn("Typeform: Missing data-form-id", container);
            return;
        }

        if (!window.tf?.createWidget) {
            console.warn("Typeform: embed script unavailable");
            container.hidden = true;
            return;
        }

        window.tf.createWidget(formId, {
            container,
            width: "100%",
            height: "100%",
            inlineOnMobile: true,
            hideHeaders: true,
            hideFooter: true,
            opacity: 0,
            hidden: {
                landing_page: window.location.pathname,
                page_title: document.title,
                referrer: document.referrer
            },
            tracking: {
                utm_source: params.get("utm_source") || "",
                utm_medium: params.get("utm_medium") || "",
                utm_campaign: params.get("utm_campaign") || "",
                utm_content: params.get("utm_content") || "",
                utm_term: params.get("utm_term") || ""
            },
            onReady() {
                container.classList.add("loaded");
                if (fallback) {
                    fallback.hidden = true;
                }
                dataLayer.push({
                    event: "typeform_loaded",
                    form_id: formId
                });
            },
            onQuestionChanged(data) {
                dataLayer.push({
                    event: "typeform_question",
                    form_id: formId,
                    question: data.ref
                });
            },
            onSubmit(data) {
                dataLayer.push({
                    event: "typeform_submit",
                    form_id: formId,
                    response_id: data.responseId
                });
            }
        });
    });
});
