'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var defineProperty = require('../../defineProperty-COGyJ6Og.js');
var React = require('react');
var ramda = require('ramda');
var reactUtils_constants_constants = require('../constants/constants.js');
var reactUtils_withImmutableActions_withImmutableActions = require('../withImmutableActions/withImmutableActions.js');
var utils_general = require('../../utils/general.js');
var zustand = require('zustand');
var shallow = require('zustand/shallow');
require('../../toPropertyKey-DW-YC76_.js');
require('../../typeof-D4ZZ_FlZ.js');
require('../../objectWithoutProperties-wSsbHFcG.js');
require('../../slicedToArray-BcL7fKuL.js');
require('../../unsupportedIterableToArray-BoHMiKNA.js');
require('../../asyncToGenerator-8vhqAwJl.js');
require('i18next');
require('@bigbinary/neeto-cist');
require('@bigbinary/neetoui/Toastr');
require('qs');

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { defineProperty._defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var getLocalStorageData = function getLocalStorageData() {
  return utils_general.getFromLocalStorage(reactUtils_constants_constants.STORAGE_KEY) || {};
};
var setLocalStorageData = function setLocalStorageData(data) {
  return utils_general.setToLocalStorage(reactUtils_constants_constants.STORAGE_KEY, data);
};

/** @type {import("neetocommons/react-utils").ZustandStoreHook} */
var useCheckpointStore = zustand.create(reactUtils_withImmutableActions_withImmutableActions(function (set) {
  return {
    checkpoints: {},
    setCheckpoint: function setCheckpoint(key, path) {
      return set(ramda.mergeDeepLeft({
        checkpoints: defineProperty._defineProperty({}, key, path)
      }));
    },
    removeCheckpoint: function removeCheckpoint(key) {
      return set(function (state) {
        return {
          checkpoints: ramda.dissoc(key, state.checkpoints)
        };
      });
    }
  };
}));
var useRegisterNavigationCheckpoint = function useRegisterNavigationCheckpoint(key) {
  var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.pathname + window.location.search;
  var persistInLocalStorage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var setCheckpoint = useCheckpointStore(ramda.prop("setCheckpoint"));
  React.useEffect(function () {
    setCheckpoint(key, path);
    if (persistInLocalStorage) {
      var localStorageData = getLocalStorageData();
      setLocalStorageData(_objectSpread(_objectSpread({}, localStorageData), {}, defineProperty._defineProperty({}, key, path)));
    }
  }, [key, path, persistInLocalStorage]);
};
var useNavigationCheckpoints = function useNavigationCheckpoints() {
  for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) {
    keys[_key] = arguments[_key];
  }
  var getCheckpoints = useCheckpointStore(shallow.useShallow(function (store) {
    return ramda.pick(keys, store.checkpoints);
  }));
  var localStorageData = getLocalStorageData();
  var localStorageCheckpoints = ramda.pick(keys, localStorageData);
  return _objectSpread(_objectSpread({}, getCheckpoints), localStorageCheckpoints);
};
var useRemoveNavigationCheckpoint = function useRemoveNavigationCheckpoint() {
  var removeCheckpoint = useCheckpointStore(ramda.prop("removeCheckpoint"));
  return function (key) {
    removeCheckpoint(key);
    var localStorageData = getLocalStorageData();
    setLocalStorageData(ramda.dissoc(key, localStorageData));
  };
};

exports.default = useRegisterNavigationCheckpoint;
exports.useNavigationCheckpoints = useNavigationCheckpoints;
exports.useRemoveNavigationCheckpoint = useRemoveNavigationCheckpoint;
//# sourceMappingURL=useRegisterNavigationCheckpoint.js.map
