(function () {
    // ---- Tab switching ----
    const tabs = document.querySelectorAll(".tls-tab");
    const panels = document.querySelectorAll(".tls-tab-panel");

    function activate(name) {
        tabs.forEach((btn) => {
            const active = btn.dataset.tab === name;
            btn.classList.toggle("is-active", active);
            btn.setAttribute("aria-selected", active ? "true" : "false");
        });
        panels.forEach((panel) => {
            panel.classList.toggle("is-active", panel.dataset.panel === name);
        });
        try {
            if (history.replaceState) {
                history.replaceState(null, "", "#" + name);
            }
        } catch (_) {}
    }

    if (tabs.length && panels.length) {
        tabs.forEach((btn) => {
            btn.addEventListener("click", () => activate(btn.dataset.tab));
        });
        const hash = (location.hash || "").replace(/^#/, "");
        const valid = ["summary", "reliability", "safety", "vulnerabilities", "compatibility", "log", "json"];
        if (valid.indexOf(hash) !== -1) {
            activate(hash);
        }
    }

    // ---- JSON-copy button (inside the JSON tab) ----
    const copyJsonBtn = document.getElementById("tls-copy-json");
    const rawJsonEl = document.getElementById("tls-raw-json");
    if (copyJsonBtn && rawJsonEl) {
        const origLabel = copyJsonBtn.textContent;
        copyJsonBtn.addEventListener("click", async () => {
            const text = rawJsonEl.textContent || "";
            try {
                if (navigator.clipboard && navigator.clipboard.writeText) {
                    await navigator.clipboard.writeText(text);
                } else {
                    const ta = document.createElement("textarea");
                    ta.value = text;
                    ta.style.position = "fixed";
                    ta.style.opacity = "0";
                    document.body.appendChild(ta);
                    ta.select();
                    try { document.execCommand("copy"); } catch (_) {}
                    document.body.removeChild(ta);
                }
                copyJsonBtn.textContent = "コピーしました";
                copyJsonBtn.classList.add("is-done");
                setTimeout(() => {
                    copyJsonBtn.textContent = origLabel;
                    copyJsonBtn.classList.remove("is-done");
                }, 1500);
            } catch (_) {
                copyJsonBtn.textContent = "コピー失敗";
                setTimeout(() => { copyJsonBtn.textContent = origLabel; }, 1500);
            }
        });
    }

    // ---- PDF / print button ----
    const printBtn = document.getElementById("tls-print-pdf");
    if (printBtn) {
        printBtn.addEventListener("click", () => {
            // Expand all tab panels so the printed PDF contains every finding,
            // then restore the previous state after the print dialog closes.
            const prev = [];
            panels.forEach((panel) => {
                prev.push([panel, panel.classList.contains("is-active")]);
                panel.classList.add("is-active");
            });
            const restore = () => {
                prev.forEach(([panel, wasActive]) => {
                    panel.classList.toggle("is-active", wasActive);
                });
                window.removeEventListener("afterprint", restore);
            };
            window.addEventListener("afterprint", restore);
            try {
                window.print();
            } catch (_) {
                restore();
            }
        });
    }

    // ---- Copy-link button ----
    const copyBtn = document.getElementById("tls-copy-link");
    if (copyBtn) {
        const origLabel = copyBtn.textContent;
        copyBtn.addEventListener("click", async () => {
            const link = copyBtn.dataset.link || location.href;
            try {
                if (navigator.clipboard && navigator.clipboard.writeText) {
                    await navigator.clipboard.writeText(link);
                } else {
                    // Fallback: use a hidden textarea + document.execCommand
                    const ta = document.createElement("textarea");
                    ta.value = link;
                    ta.style.position = "fixed";
                    ta.style.opacity = "0";
                    document.body.appendChild(ta);
                    ta.select();
                    try { document.execCommand("copy"); } catch (_) {}
                    document.body.removeChild(ta);
                }
                copyBtn.textContent = "コピーしました";
                copyBtn.classList.add("is-done");
                setTimeout(() => {
                    copyBtn.textContent = origLabel;
                    copyBtn.classList.remove("is-done");
                }, 1500);
            } catch (_) {
                copyBtn.textContent = "コピー失敗";
                setTimeout(() => { copyBtn.textContent = origLabel; }, 1500);
            }
        });
    }
})();
