{"version":3,"file":"element-theme-script.mjs","sources":["../../../../packages/theme/document-head-contents/element-theme-script.tsx"],"sourcesContent":["import { VOID_FN } from '../../utils/index.js'\nimport { themeCookieKey, ThemeName } from '../constants.js'\nimport { setThemeCookie } from '../utils/set-theme-cookie.js'\n\nexport const getThemeNameFromUrl = (): ThemeName | undefined => {\n  if (typeof window === 'undefined') return undefined\n  const url = new URL(window.location.href)\n  const queryTheme = url.searchParams.get('theme')?.toLowerCase()\n  if (queryTheme === 'light') return ThemeName.light\n  if (queryTheme === 'dark') return ThemeName.dark\n  return undefined\n}\n\n/**\n * What is happening here:\n * We want to have React dehydrated HTML to be loaded after theme is initialized.\n * That means that we need to have some code executed BEFORE main react components code,\n * before even injected script itself, so we need to provide some CSS and JS in document head\n * to read the theme from cookie and make global CSS follow preferred palette.\n * This file is providing JS code that reads/writes cookie value, including <script> element to inject\n * */\n\n/**\n *  this FN should be hermetic and has zero external items - it is inlined via .toString()\n *  __PURE__ annotation may throw an error on some external usage cases\n *  (with chances, but it is better than nothing)\n * */\n/*#__PURE__*/\nconst themeScriptValueString = function (key: string) {\n  const url = new URL(window.location.href)\n  const queryTheme = url.searchParams.get('theme')?.toLowerCase()\n  if (queryTheme === 'light' || queryTheme === 'dark') {\n    document.documentElement.dataset.lidoTheme = queryTheme\n    // eslint-disable-next-line @typescript-eslint/no-empty-function\n    return () => {}\n  }\n  if (!window.matchMedia) {\n    // eslint-disable-next-line @typescript-eslint/no-empty-function\n    return () => {}\n  }\n  const match = window.matchMedia('(prefers-color-scheme: dark)')\n  const setTheme = () => {\n    const cookieMatcher = new RegExp(`(^| )${key}=([^;]+)`)\n    const themeCookie =\n      (document.cookie.match(cookieMatcher)?.[2] as ThemeName) ?? null\n    if (themeCookie) {\n      document.documentElement.dataset.lidoTheme = themeCookie\n    } else {\n      delete document.documentElement.dataset.lidoTheme\n    }\n  }\n  setTheme()\n  match.addEventListener('change', setTheme)\n  return setTheme\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nexport let updateGlobalTheme: (theme: string) => void = VOID_FN\n\nexport let initGlobalCookieTheme =\n  typeof window === 'undefined'\n    ? VOID_FN\n    : () => {\n        const setTheme = themeScriptValueString(themeCookieKey)\n        initGlobalCookieTheme = VOID_FN\n        updateGlobalTheme = (theme: ThemeName) => {\n          setThemeCookie(theme)\n          setTheme()\n        }\n      }\n\nexport const ScriptThemeValue = () => (\n  <script\n    dangerouslySetInnerHTML={{\n      __html: `(${themeScriptValueString.toString()})(\"${themeCookieKey}\")`,\n    }}\n  />\n)\n"],"names":["getThemeNameFromUrl","window","undefined","url","URL","location","href","queryTheme","searchParams","get","toLowerCase","ThemeName","light","dark","themeScriptValueString","key","document","documentElement","dataset","lidoTheme","matchMedia","match","setTheme","cookieMatcher","RegExp","themeCookie","cookie","addEventListener","updateGlobalTheme","VOID_FN","initGlobalCookieTheme","themeCookieKey","theme","setThemeCookie","ScriptThemeValue","_jsx","dangerouslySetInnerHTML","__html","toString"],"mappings":";;;;;AAIaA,MAAAA,mBAAmB,GAAGA,MAA6B;AAC9D,EAAA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE,OAAOC,SAAS,CAAA;EACnD,MAAMC,GAAG,GAAG,IAAIC,GAAG,CAACH,MAAM,CAACI,QAAQ,CAACC,IAAI,CAAC,CAAA;AACzC,EAAA,MAAMC,UAAU,GAAGJ,GAAG,CAACK,YAAY,CAACC,GAAG,CAAC,OAAO,CAAC,EAAEC,WAAW,EAAE,CAAA;AAC/D,EAAA,IAAIH,UAAU,KAAK,OAAO,EAAE,OAAOI,SAAS,CAACC,KAAK,CAAA;AAClD,EAAA,IAAIL,UAAU,KAAK,MAAM,EAAE,OAAOI,SAAS,CAACE,IAAI,CAAA;AAChD,EAAA,OAAOX,SAAS,CAAA;AAClB,EAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMY,sBAAsB,GAAG,UAAUC,GAAW,EAAE;EACpD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