"use client";import*as t from"react";var P=["light","dark"],E="(prefers-color-scheme: dark)",Q=typeof window=="undefined",L=t.createContext(void 0),D={setTheme:e=>{},themes:[]},j=()=>{var e;return(e=t.useContext(L))!=null?e:D},z=e=>t.useContext(L)?e.children:t.createElement(O,{...e}),N=["light","dark"],O=({forcedTheme:e,disableTransitionOnChange:a=!1,enableSystem:n=!0,enableColorScheme:g=!0,storageKey:m="theme",themes:c=N,defaultTheme:o=n?"system":"light",attribute:y="data-theme",value:h,children:k,nonce:w})=>{let[i,d]=t.useState(()=>M(m,o)),[S,l]=t.useState(()=>M(m)),u=h?Object.values(h):c,R=t.useCallback(s=>{let r=s;if(!r)return;s==="system"&&n&&(r=T());let v=h?h[r]:r,C=a?_():null,x=document.documentElement;if(y==="class"?(x.classList.remove(...u),v&&x.classList.add(v)):v?x.setAttribute(y,v):x.removeAttribute(y),g){let I=P.includes(o)?o:null,A=P.includes(r)?r:I;x.style.colorScheme=A}C==null||C()},[]),f=t.useCallback(s=>{let r=typeof s=="function"?s(s):s;d(r);try{localStorage.setItem(m,r)}catch(v){}},[e]),p=t.useCallback(s=>{let r=T(s);l(r),i==="system"&&n&&!e&&R("system")},[i,e]);t.useEffect(()=>{let s=window.matchMedia(E);return s.addListener(p),p(s),()=>s.removeListener(p)},[p]),t.useEffect(()=>{let s=r=>{if(r.key!==m)return;let v=r.newValue||o;f(v)};return window.addEventListener("storage",s),()=>window.removeEventListener("storage",s)},[f]),t.useEffect(()=>{R(e!=null?e:i)},[e,i]);let $=t.useMemo(()=>({theme:i,setTheme:f,forcedTheme:e,resolvedTheme:i==="system"?S:i,themes:n?[...c,"system"]:c,systemTheme:n?S:void 0}),[i,f,e,S,n,c]);return t.createElement(L.Provider,{value:$},t.createElement(U,{forcedTheme:e,disableTransitionOnChange:a,enableSystem:n,enableColorScheme:g,storageKey:m,themes:c,defaultTheme:o,attribute:y,value:h,children:k,attrs:u,nonce:w}),k)},U=t.memo(({forcedTheme:e,storageKey:a,attribute:n,enableSystem:g,enableColorScheme:m,defaultTheme:c,value:o,attrs:y,nonce:h})=>{let k=c==="system",w=n==="class"?`var d=document.documentElement,c=d.classList;${`c.remove(${y.map(u=>`'${u}'`).join(",")})`};`:`var d=document.documentElement,n='${n}',s='setAttribute';`,i=m?(P.includes(c)?c:null)?`if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'${c}'`:"if(e==='light'||e==='dark')d.style.colorScheme=e":"",d=(l,u=!1,R=!0)=>{let f=o?o[l]:l,p=u?l+"|| ''":`'${f}'`,$="";return m&&R&&!u&&P.includes(l)&&($+=`d.style.colorScheme = '${l}';`),n==="class"?u||f?$+=`c.add(${p})`:$+="null":f&&($+=`d[s](n,${p})`),$},S=e?`!function(){${w}${d(e)}}()`:g?`!function(){try{${w}var e=localStorage.getItem('${a}');if('system'===e||(!e&&${k})){var t='${E}',m=window.matchMedia(t);if(m.media!==t||m.matches){${d("dark")}}else{${d("light")}}}else if(e){${o?`var x=${JSON.stringify(o)};`:""}${d(o?"x[e]":"e",!0)}}${k?"":"else{"+d(c,!1,!1)+"}"}${i}}catch(e){}}()`:`!function(){try{${w}var e=localStorage.getItem('${a}');if(e){${o?`var x=${JSON.stringify(o)};`:""}${d(o?"x[e]":"e",!0)}}else{${d(c,!1,!1)};}${i}}catch(t){}}();`;return t.createElement("script",{nonce:h,dangerouslySetInnerHTML:{__html:S}})}),M=(e,a)=>{if(Q)return;let n;try{n=localStorage.getItem(e)||void 0}catch(g){}return n||a},_=()=>{let e=document.createElement("style");return e.appendChild(document.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}")),document.head.appendChild(e),()=>{window.getComputedStyle(document.body),setTimeout(()=>{document.head.removeChild(e)},1)}},T=e=>(e||(e=window.matchMedia(E)),e.matches?"dark":"light");export{z as ThemeProvider,j as useTheme};
