{"version":3,"file":"initial-theme.cjs","names":[],"sources":["../../src/theme/initial-theme.ts"],"sourcesContent":["import { storage } from \"@/utils/storage\";\nimport type { ResolvedTheme, ThemeMode } from \"./types\";\n\nexport interface GetInitialThemeOptions {\n    /** localStorage key. Default: `\"tempest-theme\"`. */\n    storageKey?: string;\n    /** Theme used when nothing is stored and the user has no system preference. */\n    defaultTheme?: ThemeMode;\n}\n\n/**\n * Inline-safe theme resolution intended for early bootstrap (e.g. inside the\n * `<head>` script that prevents the flash of incorrect theme). Reads from\n * localStorage and falls back to `prefers-color-scheme`.\n */\nexport function getInitialTheme(options: GetInitialThemeOptions = {}): ResolvedTheme {\n    const { storageKey = \"tempest-theme\", defaultTheme = \"system\" } = options;\n\n    if (typeof window === \"undefined\") {\n        return defaultTheme === \"dark\" ? \"dark\" : \"light\";\n    }\n\n    const stored = storage.getRaw(storageKey) as ThemeMode | null;\n    const mode: ThemeMode = stored ?? defaultTheme;\n    if (mode === \"dark\" || mode === \"light\") return mode;\n\n    return window.matchMedia(\"(prefers-color-scheme: dark)\").matches ? \"dark\" : \"light\";\n}\n\n/**\n * Plain HTML snippet that sets `data-tempest-theme` on `<html>` before React\n * hydrates. Inline this in `<head>` to avoid a flash of the wrong theme on\n * first paint.\n *\n * @example\n * <script dangerouslySetInnerHTML={{ __html: themeInitScript() }} />\n */\nexport function themeInitScript(options: GetInitialThemeOptions = {}): string {\n    const storageKey = options.storageKey ?? \"tempest-theme\";\n    const defaultTheme = options.defaultTheme ?? \"system\";\n    return `\n(function(){try{\n  var key=${JSON.stringify(storageKey)};\n  var def=${JSON.stringify(defaultTheme)};\n  var stored=localStorage.getItem(key);\n  var mode=stored||def;\n  var resolved=mode===\"dark\"||mode===\"light\"?mode:(matchMedia(\"(prefers-color-scheme: dark)\").matches?\"dark\":\"light\");\n  document.documentElement.setAttribute(\"data-tempest-theme\",resolved);\n}catch(e){}})();\n`.trim();\n}\n"],"mappings":"wCAeA,SAAgB,EAAgB,EAAkC,CAAC,EAAkB,CACjF,GAAM,CAAE,aAAa,gBAAiB,eAAe,UAAa,EAElE,GAAI,OAAO,OAAW,IAClB,OAAO,IAAiB,OAAS,OAAS,QAI9C,IAAM,EADS,EAAA,QAAQ,OAAO,CACN,GAAU,EAGlC,OAFI,IAAS,QAAU,IAAS,QAAgB,EAEzC,OAAO,WAAW,8BAA8B,CAAC,CAAC,QAAU,OAAS,OAChF,CAUA,SAAgB,EAAgB,EAAkC,CAAC,EAAW,CAC1E,IAAM,EAAa,EAAQ,YAAc,gBACnC,EAAe,EAAQ,cAAgB,SAC7C,MAAO;;YAEC,KAAK,UAAU,CAAU,EAAE;YAC3B,KAAK,UAAU,CAAY,EAAE;;;;;;EAMvC,KAAK,CACP"}