// src/theming/useTheme.tsx
import { useCallback as useCallback2 } from "react";
import { useMemo } from "react";
import { createContext, useContext, useEffect, useState as useState2 } from "react";

// src/hooks/useLocalStorage.ts
import { useCallback, useState } from "react";

// src/utils/storage.ts
var StorageService = class {
  // this seems to be a bug in eslint as 'paramter-properties' is a special syntax of typescript
  constructor(storage) {
    this.storage = storage;
  }
  get(key) {
    const value = this.storage.getItem(key);
    if (value === null) {
      return null;
    }
    return JSON.parse(value);
  }
  set(key, value) {
    this.storage.setItem(key, JSON.stringify(value));
  }
  delete(key) {
    this.storage.removeItem(key);
  }
  deleteAll() {
    this.storage.clear();
  }
};
var LocalStorageService = class extends StorageService {
  constructor() {
    super(window.localStorage);
  }
};

// src/utils/resolveSetState.ts
function resolveSetState(action, prev) {
  return typeof action === "function" ? action(prev) : action;
}

// src/hooks/useLocalStorage.ts
var useLocalStorage = (key, initValue) => {
  const get = useCallback(() => {
    if (typeof window === "undefined") {
      return initValue;
    }
    const storageService = new LocalStorageService();
    const value = storageService.get(key);
    return value || initValue;
  }, [initValue, key]);
  const [storedValue, setStoredValue] = useState(get);
  const setValue = useCallback((action) => {
    const newValue = resolveSetState(action, storedValue);
    const storageService = new LocalStorageService();
    storageService.set(key, newValue);
    setStoredValue(newValue);
  }, [storedValue, setStoredValue, key]);
  return [storedValue, setValue];
};

// src/theming/useTheme.tsx
import { jsx } from "react/jsx-runtime";
var themes = ["light", "dark", "system"];
var defaultThemeTypeTranslation = {
  en: {
    dark: "Dark",
    light: "Light",
    system: "System",
    theme: {
      one: "Theme",
      other: "Themes"
    }
  },
  de: {
    dark: "Dunkel",
    light: "Hell",
    system: "System",
    theme: {
      one: "Farbschema",
      other: "Farbschemas"
    }
  }
};
var ThemeUtil = {
  themes,
  translation: defaultThemeTypeTranslation
};
var ThemeContext = createContext(null);
var ThemeProvider = ({ children, initialTheme }) => {
  const [storedTheme, setStoredTheme] = useLocalStorage("theme", initialTheme ?? "system");
  const [themePreference, setThemePreference] = useState2("system");
  const resolvedTheme = useMemo(() => {
    if (storedTheme && storedTheme !== "system") {
      return storedTheme;
    }
    return themePreference === "dark" ? "dark" : "light";
  }, [storedTheme, themePreference]);
  useEffect(() => {
    if (!!initialTheme && storedTheme !== initialTheme) {
      console.warn("ThemeProvider initial state changed: Prefer using useTheme's setTheme instead");
      setStoredTheme(initialTheme);
    }
  }, [initialTheme]);
  useEffect(() => {
    document.documentElement.setAttribute("data-theme", resolvedTheme);
  }, [resolvedTheme]);
  const getPreference = useCallback2(() => {
    const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
    const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches;
    setThemePreference(prefersDark ? "dark" : prefersLight ? "light" : "system");
  }, []);
  useEffect(() => {
    getPreference();
  }, [getPreference]);
  useEffect(() => {
    const darkQuery = window.matchMedia("(prefers-color-scheme: dark)");
    const lightQuery = window.matchMedia("(prefers-color-scheme: light)");
    const noPrefQuery = window.matchMedia("(prefers-color-scheme: no-preference)");
    darkQuery.addEventListener("change", getPreference);
    lightQuery.addEventListener("change", getPreference);
    noPrefQuery.addEventListener("change", getPreference);
    return () => {
      darkQuery.removeEventListener("change", getPreference);
      lightQuery.removeEventListener("change", getPreference);
      noPrefQuery.removeEventListener("change", getPreference);
    };
  }, [getPreference]);
  return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: { theme: storedTheme, resolvedTheme, setTheme: setStoredTheme }, children });
};
var useTheme = () => {
  const context = useContext(ThemeContext);
  if (!context) {
    throw new Error("useTheme must be used within ThemeContext. Try adding a ThemeProvider around your app.");
  }
  return context;
};
export {
  ThemeContext,
  ThemeProvider,
  ThemeUtil,
  useTheme
};
//# sourceMappingURL=useTheme.mjs.map