export const THEME_STORAGE_KEY = "codexRemote.theme";
export const SUPPORTED_THEMES = new Set(["system", "dark", "light"]);

export function normalizeTheme(theme) { return SUPPORTED_THEMES.has(theme) ? theme : "system"; }
export function resolveTheme(theme, darkScheme = globalThis.matchMedia?.("(prefers-color-scheme: dark)")) {
  const choice = normalizeTheme(theme);
  return choice === "system" ? (darkScheme?.matches ? "dark" : "light") : choice;
}
export function readSavedTheme(storage = globalThis.localStorage) { try { const theme = storage?.getItem(THEME_STORAGE_KEY); return SUPPORTED_THEMES.has(theme) ? theme : null; } catch { return null; } }
export function writeSavedTheme(storage = globalThis.localStorage, theme) { if (!storage || !SUPPORTED_THEMES.has(theme)) return; try { storage.setItem(THEME_STORAGE_KEY, theme); } catch {} }
