var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/theming/useTheme.tsx
var useTheme_exports = {};
__export(useTheme_exports, {
  ThemeContext: () => ThemeContext,
  ThemeProvider: () => ThemeProvider,
  ThemeUtil: () => ThemeUtil,
  useTheme: () => useTheme
});
module.exports = __toCommonJS(useTheme_exports);
var import_react2 = require("react");

// src/hooks/useLocalStorage.ts
var import_react = require("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 = (0, import_react.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] = (0, import_react.useState)(get);
  const setValue = (0, import_react.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
var import_jsx_runtime = require("react/jsx-runtime");
var themes = ["light", "dark", "system"];
var ThemeUtil = {
  themes
};
var ThemeContext = (0, import_react2.createContext)(null);
var ThemeProvider = ({ children, theme }) => {
  const {
    value: storedTheme,
    setValue: setStoredTheme,
    deleteValue: deleteStoredTheme
  } = useLocalStorage("theme", "system");
  const [themePreference, setThemePreference] = (0, import_react2.useState)("system");
  const resolvedTheme = (0, import_react2.useMemo)(() => {
    if (theme && theme !== "system") {
      return theme;
    }
    if (storedTheme && storedTheme !== "system") {
      return storedTheme;
    }
    if (themePreference !== "system") {
      return themePreference;
    }
    return "light";
  }, [storedTheme, theme, themePreference]);
  (0, import_react2.useEffect)(() => {
    if (!theme) return;
    if (theme === "system") {
      deleteStoredTheme();
    } else {
      setStoredTheme(theme);
    }
  }, [theme]);
  (0, import_react2.useEffect)(() => {
    document.documentElement.setAttribute("data-theme", resolvedTheme);
  }, [resolvedTheme]);
  const getPreference = (0, import_react2.useCallback)(() => {
    const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
    const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches;
    setThemePreference(prefersDark ? "dark" : prefersLight ? "light" : "system");
  }, []);
  (0, import_react2.useEffect)(() => {
    getPreference();
  }, [getPreference]);
  (0, import_react2.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__ */ (0, import_jsx_runtime.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 = (0, import_react2.useContext)(ThemeContext);
  if (!context) {
    throw new Error("useTheme must be used within ThemeContext. Try adding a ThemeProvider around your app.");
  }
  return context;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ThemeContext,
  ThemeProvider,
  ThemeUtil,
  useTheme
});
//# sourceMappingURL=useTheme.cjs.map