"use client";
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/hooks/useLocalStorage.ts
var useLocalStorage_exports = {};
__export(useLocalStorage_exports, {
  useLocalStorage: () => useLocalStorage
});
module.exports = __toCommonJS(useLocalStorage_exports);
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 };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useLocalStorage
});
//# sourceMappingURL=useLocalStorage.cjs.map