"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.asyncLocalStorage = asyncLocalStorage;
exports.useLocalStorage = void 0;
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// Plain key-scoped AsyncStorage helper (NOT a React hook). Callable from
// reducers, helpers, and React code alike. The `use*` name (now
// `asyncLocalStorage`) was renamed to stop ESLint's `react-hooks/rules-of-hooks`
// firing in every non-React call site. `useLocalStorage` is kept below
// as a back-compat alias.
function asyncLocalStorage(key) {
  const get = async () => {
    try {
      const storedValue = await _asyncStorage.default.getItem(key);
      if (!storedValue) {
        return null;
      }
      return JSON.parse(storedValue);
    } catch (error) {
      console.error('Failed to read from AsyncStorage', error);
      return null;
    }
  };
  const set = async value => {
    try {
      const stringValue = JSON.stringify(value);
      await _asyncStorage.default.setItem(key, stringValue);
    } catch (error) {
      console.error('Failed to write to AsyncStorage', error);
    }
  };
  const update = async updates => {
    try {
      const currentValue = await get();
      const newValue = currentValue ? {
        ...currentValue,
        ...updates
      } : updates;
      await set(newValue);
    } catch (error) {
      console.error('Failed to update AsyncStorage value', error);
    }
  };
  const remove = async () => {
    try {
      await _asyncStorage.default.removeItem(key);
    } catch (error) {
      console.error('Failed to remove AsyncStorage value', error);
    }
  };
  return {
    get,
    set,
    update,
    remove
  };
}

// Back-compat alias for old call sites. New code: use asyncLocalStorage.
const useLocalStorage = exports.useLocalStorage = asyncLocalStorage;
//# sourceMappingURL=useLocalStorage.js.map