function loadTheme() {
    let theme = localStorage.getItem("theme");
    if (!theme) {
        if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
            theme = "dark";
        } else {
            theme = "light";
        }
    }

    return theme;
}

let currentTheme = $state(loadTheme());

export function toggleTheme() {
    const newTheme = currentTheme === "dark" ? "light" : "dark";
    localStorage.setItem("theme", newTheme);
    currentTheme = newTheme;
}

export function getTheme(): string {
    return currentTheme;
}
