import { _ as _slicedToArray } from '../slicedToArray-BB-AwwkK.js';
import React__default from 'react';

function useLocalStorage(key) {
  var initialValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

  var _React$useState = React__default.useState(function () {
    try {
      var storedItem = window.localStorage.getItem(key);
      return storedItem ? JSON.parse(storedItem) : initialValue;
    } catch (error) {
      // eslint-disable-next-line no-console
      console.log(error, "local storage not working");
      return initialValue;
    }
  }),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      item = _React$useState2[0],
      setItem = _React$useState2[1];

  var setValue = function setValue(value) {
    var storableValue = value instanceof Function ? value(item) : value;
    setItem(storableValue);
    window.localStorage.setItem(key, JSON.stringify(storableValue));
  };

  return [item, setValue];
}

export { useLocalStorage as default };
