{"version":3,"sources":["../../src/theming/useTheme.tsx","../../src/util/noop.ts","../../src/hooks/useLocalStorage.ts","../../src/util/storage.ts","../../src/util/resolveSetState.ts"],"sourcesContent":["import type { Dispatch, PropsWithChildren, SetStateAction } from 'react'\nimport { useMemo } from 'react'\nimport { createContext, useContext, useEffect, useState } from 'react'\nimport type { Translation, TranslationPlural } from '../localization/useTranslation'\nimport { noop } from '../util/noop'\nimport { useLocalStorage } from '../hooks/useLocalStorage'\n\nconst themes = ['light', 'dark', 'system'] as const\n\nexport type ThemeType = typeof themes[number]\n\nexport type ThemeTypeTranslation = Record<ThemeType, string> & {\n  theme: TranslationPlural,\n}\n\nconst defaultThemeTypeTranslation: Translation<ThemeTypeTranslation> = {\n  en: {\n    dark: 'Dark',\n    light: 'Light',\n    system: 'System',\n    theme: {\n      one: 'Theme',\n      other: 'Themes'\n    }\n  },\n  de: {\n    dark: 'Dunkel',\n    light: 'Hell',\n    system: 'System',\n    theme: {\n      one: 'Farbschema',\n      other: 'Farbschemas'\n    }\n  }\n}\n\nexport const ThemeUtil = {\n  themes,\n  translation: defaultThemeTypeTranslation,\n}\n\ntype ThemeContextType = {\n  theme: ThemeType,\n  setTheme: Dispatch<SetStateAction<ThemeType>>,\n}\n\nexport const ThemeContext = createContext<ThemeContextType>({\n  theme: 'light',\n  setTheme: noop\n})\n\ntype ThemeProviderProps = {\n  initialTheme?: ThemeType,\n}\n\nexport const ThemeProvider = ({ children, initialTheme }: PropsWithChildren<ThemeProviderProps>) => {\n  const [storedTheme, setStoredTheme] = useLocalStorage<ThemeType>('theme', initialTheme ?? 'system')\n  const [userTheme, setUserTheme] = useState<ThemeType>()\n\n  useEffect(() => {\n    if (!!initialTheme && storedTheme !== initialTheme) {\n      console.warn('ThemeProvider initial state changed: Prefer using useTheme\\'s setTheme instead')\n      setStoredTheme(initialTheme)\n    }\n  }, [initialTheme]) // eslint-disable-line react-hooks/exhaustive-deps\n\n  const usedTheme = useMemo(() => storedTheme !== 'system' ? storedTheme : userTheme, [storedTheme, userTheme])\n\n  useEffect(() => {\n    document.documentElement.setAttribute('data-theme', usedTheme)\n  }, [usedTheme])\n\n  useEffect(() => {\n    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches\n    setUserTheme(prefersDark ? 'dark' : 'light')\n  }, [])\n\n  return (\n    <ThemeContext.Provider value={{ theme: storedTheme, setTheme: setStoredTheme }}>\n      {children}\n    </ThemeContext.Provider>\n  )\n}\n\n\nexport const useTheme = () => useContext(ThemeContext)\n","export const noop = () => undefined\n","'use client'\n\nimport type { Dispatch, SetStateAction } from 'react'\nimport { useCallback, useState } from 'react'\nimport { LocalStorageService } from '../util/storage'\nimport { resolveSetState } from '../util/resolveSetState'\n\ntype SetValue<T> = Dispatch<SetStateAction<T>>\nexport const useLocalStorage = <T>(key: string, initValue: T): [T, SetValue<T>] => {\n  const get = useCallback((): T => {\n    if (typeof window === 'undefined') {\n      return initValue\n    }\n    const storageService = new LocalStorageService()\n    const value = storageService.get<T>(key)\n    return value || initValue\n  }, [initValue, key])\n\n  const [storedValue, setStoredValue] = useState<T>(get)\n\n  const setValue: SetValue<T> = useCallback(action => {\n    const newValue = resolveSetState(action, storedValue)\n    const storageService = new LocalStorageService()\n    storageService.set(key, newValue)\n\n    setStoredValue(newValue)\n  }, [storedValue, setStoredValue, key])\n\n  return [storedValue, setValue]\n}","class StorageService {\n  // this seems to be a bug in eslint as 'paramter-properties' is a special syntax of typescript\n\n  constructor(private storage: Storage) {\n  }\n\n  public get<T>(key: string): T | null {\n    const value = this.storage.getItem(key)\n    if (value === null) {\n      return null\n    }\n    return JSON.parse(value)\n  }\n\n  public set<T>(key: string, value: T) {\n    this.storage.setItem(key, JSON.stringify(value))\n  }\n\n  public delete(key: string) {\n    this.storage.removeItem(key)\n  }\n\n  public deleteAll() {\n    this.storage.clear()\n  }\n}\n\nexport class LocalStorageService extends StorageService {\n  constructor() {\n    super(window.localStorage)\n  }\n}\n\nexport class SessionStorageService extends StorageService {\n  constructor() {\n    super(window.sessionStorage)\n  }\n}\n","import type { SetStateAction } from 'react'\n\nexport function resolveSetState<T>(action: SetStateAction<T>, prev: T): T {\n  return typeof action === 'function' ? (action as (prev: T) => T)(prev) : action\n}"],"mappings":";AACA,SAAS,eAAe;AACxB,SAAS,eAAe,YAAY,WAAW,YAAAA,iBAAgB;;;ACFxD,IAAM,OAAO,MAAM;;;ACG1B,SAAS,aAAa,gBAAgB;;;ACHtC,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAGnB,YAAoB,SAAkB;AAAlB;AAAA,EACpB;AAAA,EAEO,IAAO,KAAuB;AACnC,UAAM,QAAQ,KAAK,QAAQ,QAAQ,GAAG;AACtC,QAAI,UAAU,MAAM;AAClB,aAAO;AAAA,IACT;AACA,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEO,IAAO,KAAa,OAAU;AACnC,SAAK,QAAQ,QAAQ,KAAK,KAAK,UAAU,KAAK,CAAC;AAAA,EACjD;AAAA,EAEO,OAAO,KAAa;AACzB,SAAK,QAAQ,WAAW,GAAG;AAAA,EAC7B;AAAA,EAEO,YAAY;AACjB,SAAK,QAAQ,MAAM;AAAA,EACrB;AACF;AAEO,IAAM,sBAAN,cAAkC,eAAe;AAAA,EACtD,cAAc;AACZ,UAAM,OAAO,YAAY;AAAA,EAC3B;AACF;;;AC7BO,SAAS,gBAAmB,QAA2B,MAAY;AACxE,SAAO,OAAO,WAAW,aAAc,OAA0B,IAAI,IAAI;AAC3E;;;AFIO,IAAM,kBAAkB,CAAI,KAAa,cAAmC;AACjF,QAAM,MAAM,YAAY,MAAS;AAC/B,QAAI,OAAO,WAAW,aAAa;AACjC,aAAO;AAAA,IACT;AACA,UAAM,iBAAiB,IAAI,oBAAoB;AAC/C,UAAM,QAAQ,eAAe,IAAO,GAAG;AACvC,WAAO,SAAS;AAAA,EAClB,GAAG,CAAC,WAAW,GAAG,CAAC;AAEnB,QAAM,CAAC,aAAa,cAAc,IAAI,SAAY,GAAG;AAErD,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