import { cache } from "react";
import { cookies } from "next/headers";

export type Theme = "light" | "dark";
export const THEME_COOKIE = "absher_theme";

export function isTheme(value: unknown): value is Theme {
  return value === "light" || value === "dark";
}

export const getTheme = cache(async (): Promise<Theme> => {
  const value = (await cookies()).get(THEME_COOKIE)?.value;
  return isTheme(value) ? value : "light";
});
