import { createContext, useContext, useEffect, useState } from "react"type Theme = "dark" | "light" | "system"type ResolvedTheme = "dark" | "light"type ThemeProviderProps = {children: React.ReactNodedefaultTheme?: ThemestorageKey?: string}type ThemeProviderState = {theme: ThemeresolvedTheme: ResolvedThemesetTheme: (theme: Theme) => void}const initialState: ThemeProviderState = {theme: "system",resolvedTheme: "light",setTheme: () => null,}const ThemeProviderContext = createContext<ThemeProviderState>(initialState)export function ThemeProvider({children,defaultTheme = "system",storageKey = "ui-theme",...props}: ThemeProviderProps) {const [theme, setTheme] = useState<Theme>(() => (localStorage.getItem(storageKey) as Theme) || defaultTheme)const [systemDark, setSystemDark] = useState(() => window.matchMedia("(prefers-color-scheme: dark)").matches)useEffect(() => {const media = window.matchMedia("(prefers-color-scheme: dark)")const onChange = (event: MediaQueryListEvent) => setSystemDark(event.matches)media.addEventListener("change", onChange)return () => media.removeEventListener("change", onChange)}, [])const resolvedTheme = theme === "system" ? (systemDark ? "dark" : "light") : themeuseEffect(() => {const root = window.document.documentElementroot.classList.remove("light", "dark")root.classList.add(resolvedTheme)}, [resolvedTheme])const value = {theme,resolvedTheme,setTheme: (theme: Theme) => {localStorage.setItem(storageKey, theme)setTheme(theme)},}return (<ThemeProviderContext.Provider {...props} value={value}>{children}</ThemeProviderContext.Provider>)}export const useTheme = () => useContext(ThemeProviderContext)