"use client";
import { Fragment, jsx } from "react/jsx-runtime";
import * as React from "react";
import { createContext, useContext } from "react";
const ThemeContext = createContext(void 0);
const THEME_STORAGE_KEY = "om-theme";
function getSystemTheme() {
  if (typeof window === "undefined") return "light";
  return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
function getStoredTheme() {
  if (typeof window === "undefined") return "system";
  try {
    const stored = localStorage.getItem(THEME_STORAGE_KEY);
    if (stored === "light" || stored === "dark" || stored === "system") {
      return stored;
    }
  } catch (error) {
    if (process.env.NODE_ENV === "development") {
      console.warn("[ThemeProvider] localStorage read failed:", error);
    }
  }
  return "system";
}
function applyTheme(resolvedTheme) {
  const root = document.documentElement;
  if (resolvedTheme === "dark") {
    root.classList.add("dark");
  } else {
    root.classList.remove("dark");
  }
}
function ThemeProvider({ children }) {
  const [theme, setThemeState] = React.useState("system");
  const [resolvedTheme, setResolvedTheme] = React.useState("light");
  const [mounted, setMounted] = React.useState(false);
  React.useEffect(() => {
    const stored = getStoredTheme();
    setThemeState(stored);
    const resolved = stored === "system" ? getSystemTheme() : stored;
    setResolvedTheme(resolved);
    applyTheme(resolved);
    setMounted(true);
  }, []);
  React.useEffect(() => {
    if (typeof window === "undefined") return;
    const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
    const handleChange = () => {
      if (theme === "system") {
        const newResolved = getSystemTheme();
        setResolvedTheme(newResolved);
        applyTheme(newResolved);
      }
    };
    mediaQuery.addEventListener("change", handleChange);
    return () => mediaQuery.removeEventListener("change", handleChange);
  }, [theme]);
  const setTheme = React.useCallback((newTheme) => {
    setThemeState(newTheme);
    try {
      localStorage.setItem(THEME_STORAGE_KEY, newTheme);
    } catch (error) {
      if (process.env.NODE_ENV === "development") {
        console.warn("[ThemeProvider] localStorage write failed:", error);
      }
    }
    const resolved = newTheme === "system" ? getSystemTheme() : newTheme;
    setResolvedTheme(resolved);
    applyTheme(resolved);
  }, []);
  const value = React.useMemo(
    () => ({ theme, resolvedTheme, setTheme }),
    [theme, resolvedTheme, setTheme]
  );
  if (!mounted) {
    return /* @__PURE__ */ jsx(Fragment, { children });
  }
  return /* @__PURE__ */ jsx(ThemeContext.Provider, { value, children });
}
function useTheme() {
  const context = useContext(ThemeContext);
  if (context === void 0) {
    return {
      theme: "system",
      resolvedTheme: "light",
      setTheme: () => {
      }
    };
  }
  return context;
}
export {
  ThemeProvider,
  useTheme
};
//# sourceMappingURL=ThemeProvider.js.map
