// 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/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((value) => {
    const newValue = value instanceof Function ? value(storedValue) : value;
    const storageService = new LocalStorageService();
    storageService.set(key, value);
    setStoredValue(newValue);
  }, [storedValue, setStoredValue, key]);
  useEffect(() => {
    setStoredValue(get());
  }, []);
  return [storedValue, setValue];
};
export {
  useLocalStorage
};
//# sourceMappingURL=useLocalStorage.mjs.map