import { LS, readMerged, writeJSON } from "./storage";

export type Mode = "dark" | "light";
export type Scheme = "midnight" | "forest" | "slate" | "youtube";

export const SCHEMES: { id: Scheme; name: string; swatch: string }[] = [
  { id: "midnight", name: "Midnight", swatch: "#6d8cff" },
  { id: "forest", name: "Forest", swatch: "#2dd4bf" },
  { id: "slate", name: "Slate", swatch: "#e8913c" },
  { id: "youtube", name: "YouTube", swatch: "#ff3b46" },
];

export interface ThemePrefs {
  mode: Mode;
  scheme: Scheme;
  fontScale: number;
}

export const DEFAULT_THEME: ThemePrefs = {
  mode: "dark",
  scheme: "midnight",
  fontScale: 1.06,
};

export function applyTheme(t: ThemePrefs): void {
  const el = document.documentElement;
  el.dataset.theme = t.mode;
  el.dataset.scheme = t.scheme;
  el.style.setProperty("--font-scale", String(t.fontScale));
}

export function loadLocalTheme(): ThemePrefs {
  return readMerged(LS.theme, DEFAULT_THEME);
}

export function saveLocalTheme(t: ThemePrefs): void {
  writeJSON(LS.theme, t);
}
