// src/theming/useTheme.tsx
import { createContext, useCallback as useCallback2, useContext, useEffect, useMemo, 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, backupValue) => {
  const get = useCallback(() => {
    if (typeof window === "undefined") {
      return backupValue;
    }
    const storageService = new LocalStorageService();
    try {
      const value = storageService.get(key);
      return value || backupValue;
    } catch (_) {
      console.warn("useLocalStorage: Error while reading the stored value. Make sure your typing is correct.");
      storageService.delete(key);
      console.info(`useLocalStorage: deleted erroneous value for key: ${key}`);
      return backupValue;
    }
  }, [backupValue, 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]);
  const deleteValue = () => {
    const storageService = new LocalStorageService();
    storageService.delete(key);
    setStoredValue(backupValue);
  };
  return { value: storedValue, setValue, deleteValue };
};

// src/theming/useTheme.tsx
import { jsx } from "react/jsx-runtime";
var themes = ["light", "dark", "system"];
var ThemeUtil = {
  themes
};
var ThemeContext = createContext(null);
var ThemeProvider = ({ children, theme }) => {
  const {
    value: storedTheme,
    setValue: setStoredTheme,
    deleteValue: deleteStoredTheme
  } = useLocalStorage("theme", "system");
  const [themePreference, setThemePreference] = useState2("system");
  const resolvedTheme = useMemo(() => {
    if (theme && theme !== "system") {
      return theme;
    }
    if (storedTheme && storedTheme !== "system") {
      return storedTheme;
    }
    if (themePreference !== "system") {
      return themePreference;
    }
    return "light";
  }, [storedTheme, theme, themePreference]);
  useEffect(() => {
    if (!theme) return;
    if (theme === "system") {
      deleteStoredTheme();
    } else {
      setStoredTheme(theme);
    }
  }, [theme]);
  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: (newTheme) => {
          if (theme && theme !== "system") {
            console.warn("ThemeProvider: Attempting to change the theme while setting a fixed theme won't have any effect. Change the theme provided to the ThemeProvider instead.");
          }
          setStoredTheme(newTheme);
        }
      },
      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.js.map