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

// src/util/noop.ts
var noop = () => void 0;

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

// src/util/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/util/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]);
  useEffect(() => {
    setStoredValue(get());
  }, []);
  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({
  theme: "light",
  setTheme: noop
});
var ThemeProvider = ({ children, initialTheme = "system" }) => {
  const [storedTheme, setStoredTheme] = useLocalStorage("theme", initialTheme);
  const [userTheme, setUserTheme] = useState2();
  useEffect2(() => {
    if (storedTheme !== initialTheme) {
      console.warn("ThemeProvider initial state changed: Prefer using useTheme's setTheme instead");
      setStoredTheme(initialTheme);
    }
  }, [initialTheme]);
  const usedTheme = useMemo(() => storedTheme !== "system" ? storedTheme : userTheme, [storedTheme, userTheme]);
  useEffect2(() => {
    document.documentElement.setAttribute("data-theme", usedTheme);
  }, [usedTheme]);
  useEffect2(() => {
    const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
    setUserTheme(prefersDark ? "dark" : "light");
  }, []);
  return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: { theme: storedTheme, setTheme: setStoredTheme }, children });
};
var useTheme = () => useContext(ThemeContext);
export {
  ThemeContext,
  ThemeProvider,
  ThemeUtil,
  useTheme
};
//# sourceMappingURL=useTheme.mjs.map