{"version":3,"file":"mode-watcher-DyhmqlyS.js","sources":["../../../.svelte-kit/adapter-node/chunks/mode-watcher.js"],"sourcesContent":["import { c as create_ssr_component, d as add_attribute } from \"./ssr.js\";\nimport { d as derived, w as writable } from \"./index2.js\";\nlet timeoutAction;\nlet timeoutEnable;\nfunction withoutTransition(action) {\n if (typeof document === \"undefined\")\n return;\n clearTimeout(timeoutAction);\n clearTimeout(timeoutEnable);\n const style = document.createElement(\"style\");\n const css = document.createTextNode(`* {\n -webkit-transition: none !important;\n -moz-transition: none !important;\n -o-transition: none !important;\n -ms-transition: none !important;\n transition: none !important;\n }`);\n style.appendChild(css);\n const disable = () => document.head.appendChild(style);\n const enable = () => document.head.removeChild(style);\n if (typeof window.getComputedStyle !== \"undefined\") {\n disable();\n action();\n window.getComputedStyle(style).opacity;\n enable();\n return;\n }\n if (typeof window.requestAnimationFrame !== \"undefined\") {\n disable();\n action();\n window.requestAnimationFrame(enable);\n return;\n }\n disable();\n timeoutAction = window.setTimeout(() => {\n action();\n timeoutEnable = window.setTimeout(enable, 120);\n }, 120);\n}\nconst noopStorage = {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n getItem: (_key) => null,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n setItem: (_key, _value) => {\n }\n};\nconst isBrowser = typeof document !== \"undefined\";\nconst modes = [\"dark\", \"light\", \"system\"];\nconst localStorageKey = \"mode-watcher-mode\";\nconst userPrefersMode = createUserPrefersMode();\nconst systemPrefersMode = createSystemMode();\nconst themeColors = writable(void 0);\nconst disableTransitions = writable(true);\nconst derivedMode = createDerivedMode();\nfunction createUserPrefersMode() {\n const defaultValue = \"system\";\n const storage = isBrowser ? localStorage : noopStorage;\n const initialValue = storage.getItem(localStorageKey);\n let value = isValidMode(initialValue) ? initialValue : defaultValue;\n const { subscribe, set: _set } = writable(value, () => {\n if (!isBrowser)\n return;\n const handler = (e) => {\n if (e.key !== localStorageKey)\n return;\n const newValue = e.newValue;\n if (isValidMode(newValue)) {\n _set(value = newValue);\n } else {\n _set(value = defaultValue);\n }\n };\n addEventListener(\"storage\", handler);\n return () => removeEventListener(\"storage\", handler);\n });\n function set(v) {\n _set(value = v);\n storage.setItem(localStorageKey, value);\n }\n return {\n subscribe,\n set\n };\n}\nfunction createSystemMode() {\n const defaultValue = void 0;\n let track = true;\n const { subscribe, set } = writable(defaultValue, () => {\n if (!isBrowser)\n return;\n const handler = (e) => {\n if (!track)\n return;\n set(e.matches ? \"light\" : \"dark\");\n };\n const mediaQueryState = window.matchMedia(\"(prefers-color-scheme: light)\");\n mediaQueryState.addEventListener(\"change\", handler);\n return () => mediaQueryState.removeEventListener(\"change\", handler);\n });\n function query() {\n if (!isBrowser)\n return;\n const mediaQueryState = window.matchMedia(\"(prefers-color-scheme: light)\");\n set(mediaQueryState.matches ? \"light\" : \"dark\");\n }\n function tracking(active) {\n track = active;\n }\n return {\n subscribe,\n query,\n tracking\n };\n}\nfunction createDerivedMode() {\n const { subscribe } = derived([userPrefersMode, systemPrefersMode, themeColors, disableTransitions], ([$userPrefersMode, $systemPrefersMode, $themeColors, $disableTransitions]) => {\n if (!isBrowser)\n return void 0;\n const derivedMode2 = $userPrefersMode === \"system\" ? $systemPrefersMode : $userPrefersMode;\n function update() {\n const htmlEl = document.documentElement;\n const themeColorEl = document.querySelector('meta[name=\"theme-color\"]');\n if (derivedMode2 === \"light\") {\n htmlEl.classList.remove(\"dark\");\n htmlEl.style.colorScheme = \"light\";\n if (themeColorEl && $themeColors) {\n themeColorEl.setAttribute(\"content\", $themeColors.light);\n }\n } else {\n htmlEl.classList.add(\"dark\");\n htmlEl.style.colorScheme = \"dark\";\n if (themeColorEl && $themeColors) {\n themeColorEl.setAttribute(\"content\", $themeColors.dark);\n }\n }\n }\n if ($disableTransitions) {\n withoutTransition(update);\n } else {\n update();\n }\n return derivedMode2;\n });\n return {\n subscribe\n };\n}\nfunction isValidMode(value) {\n if (typeof value !== \"string\")\n return false;\n return modes.includes(value);\n}\nfunction setInitialMode(defaultMode, themeColors2) {\n const rootEl = document.documentElement;\n const mode = localStorage.getItem(\"mode-watcher-mode\") || defaultMode;\n const light = mode === \"light\" || mode === \"system\" && window.matchMedia(\"(prefers-color-scheme: light)\").matches;\n rootEl.classList[light ? \"remove\" : \"add\"](\"dark\");\n rootEl.style.colorScheme = light ? \"light\" : \"dark\";\n if (themeColors2) {\n const themeMetaEl = document.querySelector('meta[name=\"theme-color\"]');\n if (themeMetaEl) {\n themeMetaEl.setAttribute(\"content\", mode === \"light\" ? themeColors2.light : themeColors2.dark);\n }\n }\n localStorage.setItem(\"mode-watcher-mode\", mode);\n}\nconst Mode_watcher = create_ssr_component(($$result, $$props, $$bindings, slots) => {\n let { track = true } = $$props;\n let { defaultMode = \"system\" } = $$props;\n let { themeColors: themeColors$1 = void 0 } = $$props;\n let { disableTransitions: disableTransitions$1 = true } = $$props;\n themeColors.set(themeColors$1);\n disableTransitions.set(disableTransitions$1);\n const args = `\"${defaultMode}\"${themeColors$1 ? `, ${JSON.stringify(themeColors$1)}` : \"\"}`;\n if ($$props.track === void 0 && $$bindings.track && track !== void 0)\n $$bindings.track(track);\n if ($$props.defaultMode === void 0 && $$bindings.defaultMode && defaultMode !== void 0)\n $$bindings.defaultMode(defaultMode);\n if ($$props.themeColors === void 0 && $$bindings.themeColors && themeColors$1 !== void 0)\n $$bindings.themeColors(themeColors$1);\n if ($$props.disableTransitions === void 0 && $$bindings.disableTransitions && disableTransitions$1 !== void 0)\n $$bindings.disableTransitions(disableTransitions$1);\n return `${$$result.head += `${themeColors$1 ? ` ` : ``}${`