const button = document.getElementById("theme");

function theme_get() {
    return document.documentElement.getAttribute("data-theme");
}
function theme_load() {
    const theme = localStorage.getItem("theme");
    if (theme) {
        theme_set(theme);
    }
}
function theme_set(theme) {
    document.documentElement.setAttribute("data-theme", theme);
    button.innerHTML = theme;
    theme_write(theme);
}
function theme_swap(theme) {
    return theme === "dark" ? "light" : "dark";
}
function theme_toggle() {
    theme_set(theme_swap(theme_get()));
}
function theme_write(theme) {
    localStorage.setItem("theme", theme);
}

button.addEventListener("click", () => {
    theme_toggle();
});

theme_load();
