export type ThemeMode = "paper" | "night";

export const DEFAULT_THEME_MODE: ThemeMode = "paper";
export const THEME_STORAGE_KEY = "ww.theme_mode";

export function isThemeMode(value: unknown): value is ThemeMode {
  return value === "paper" || value === "night";
}

export function normalizeThemeMode(value: unknown): ThemeMode {
  return isThemeMode(value) ? value : DEFAULT_THEME_MODE;
}

export function nextThemeMode(current: ThemeMode): ThemeMode {
  return current === "night" ? "paper" : "night";
}

export function themeModeLabel(mode: ThemeMode): string {
  return mode === "night" ? "夜读模式" : "纸感模式";
}

export function themeToggleLabel(mode: ThemeMode): string {
  return mode === "night" ? "切换到纸感模式" : "切换到夜读模式";
}

export function themeBootstrapScript(): string {
  return `
try {
  var mode = localStorage.getItem(${JSON.stringify(THEME_STORAGE_KEY)});
  if (mode !== "paper" && mode !== "night") mode = ${JSON.stringify(DEFAULT_THEME_MODE)};
  document.documentElement.dataset.theme = mode;
} catch (error) {
  document.documentElement.dataset.theme = ${JSON.stringify(DEFAULT_THEME_MODE)};
}
`.trim();
}
