const KEY = "gitbound.theme";
const MODES = new Set(["system", "light", "dark"]);
const ACCENTS = new Set(["moss", "violet"]);
const FALLBACK = { mode: "system", accent: "moss" };
export function readTheme() {
try {
const raw = window.localStorage.getItem(KEY);
if (!raw) return { ...FALLBACK };
const parsed = JSON.parse(raw);
return {
mode: MODES.has(parsed.mode) ? parsed.mode : FALLBACK.mode,
accent: ACCENTS.has(parsed.accent) ? parsed.accent : FALLBACK.accent,
};
} catch {
return { ...FALLBACK };
}
}
export function applyTheme(mode, accent) {
const applied = {
mode: MODES.has(mode) ? mode : FALLBACK.mode,
accent: ACCENTS.has(accent) ? accent : FALLBACK.accent,
};
const root = document.documentElement;
if (applied.mode === "system") root.removeAttribute("data-theme");
else root.setAttribute("data-theme", applied.mode);
if (applied.accent === "moss") root.removeAttribute("data-accent");
else root.setAttribute("data-accent", applied.accent);
try {
window.localStorage.setItem(KEY, JSON.stringify(applied));
} catch {
}
return applied;
}