document.addEventListener("DOMContentLoaded", () => {
    const links = document.getElementsByTagName("a");
    for (let i = 0; i < links.length; i++) {
        links[i].addEventListener("click", (event) => {
            if (0 === event.button) {
                onClickLink(links[i].href);
                event.preventDefault();
            }
        }, false);

        links[i].addEventListener("keydown", (event) => {
            if (event.key && "enter" === event.key.toLowerCase()) {
                onClickLink(links[i].href);
                event.preventDefault();
            }
        }, false);
    }

    const images = document.querySelectorAll(".contents-image-zoomable");
    for (let i = 0; i < images.length; i++) {
        images[i].addEventListener("click", (event) => {
            if (0 === event.button) {
                window.parent.postMessage({
                    type: "zoom",
                    data: {
                        url: images[i].src
                    }
                }, "*");
            }
        }, false);
    }

    window.parent.postMessage({
        type: "title",
        data: {
            title: document.title
        }
    }, "*");

    if (window.matchMedia) {
        // テーマをWindowsに合わせる
        if (window.matchMedia("(prefers-color-scheme: dark)").matches)
            setTheme("dark");
        else
            setTheme("light");

        // テーマの変更を監視
        window.matchMedia("(prefers-color-scheme: dark)").addListener((mql) => {
            setTheme(mql.matches ? "dark" : "light");
        });
    } else
        setTheme("light");
}, false);

// リンクがクリックまたはエンターキーで押されたとき
function onClickLink(href) {
    const filename1 = window.location.href.match(".+/(.+?)([\?#;].*)?$")[1];
    const filename2 = href.match(".+/(.+?)([\?#;].*)?$")[1];

    if (filename1 === filename2) {
        const url = new URL(href);
        const hash = "" !== url.hash ? url.hash.slice(1) : "";

        const element = document.getElementById("" !== hash ? hash : "top");
        if (element) {
            if (element.scrollIntoView) {
                element.scrollIntoView({
                    behavior: "smooth",
                    block: "start",
                    inline: "start"
                });
            } else
                window.scrollTo(element.clientTop);
        }
    } else {
        window.parent.postMessage({
            type: "navigate",
            data: {
                url: href
            }
        }, "*");
    }
}

// テーマの変更
function setTheme(theme) {
    document.getElementById("css-theme").href = "../css/theme-" + theme + ".css";
}

// メッセージを受信
window.addEventListener("message", (response) => {
    if ("scroll" === response.data.type) {
        const element = document.getElementById(response.data.data.id);

        if (element) {
            if (element.scrollIntoView) {
                element.scrollIntoView({
                    behavior: "smooth",
                    block: "start",
                    inline: "start"
                });
            } else
                window.scrollTo(element.clientTop);
        }
    }
}, false);
