Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/components/filter/hooks/useTimeRangeFetch.es", ['module', 'exports', 'require', 'frontend-js-react-web$react', '../../../shared/components/filter/FilterContext.es', '../../../shared/components/filter/util/filterConstants.es', '../../../shared/hooks/useBeforeUnload.es', '../../../shared/hooks/useStorage.es', '../../AppContext.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useTimeRangeFetch = void 0;

    var _react = require("frontend-js-react-web$react");

    var _FilterContext = require("../../../shared/components/filter/FilterContext.es");

    var _filterConstants = _interopRequireDefault(require("../../../shared/components/filter/util/filterConstants.es"));

    var _useBeforeUnload = require("../../../shared/hooks/useBeforeUnload.es");

    var _useStorage = require("../../../shared/hooks/useStorage.es");

    var _AppContext = require("../../AppContext.es");

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { "default": obj };
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    var useTimeRangeFetch = function useTimeRangeFetch() {
      var _useContext = (0, _react.useContext)(_AppContext.AppContext),
          client = _useContext.client;

      var _useContext2 = (0, _react.useContext)(_FilterContext.FilterContext),
          dispatchFilterError = _useContext2.dispatchFilterError;

      var _useSessionStorage = (0, _useStorage.useSessionStorage)('timeRanges'),
          _useSessionStorage2 = _slicedToArray(_useSessionStorage, 3),
          update = _useSessionStorage2[1],
          remove = _useSessionStorage2[2];

      var clean = function clean() {
        dispatchFilterError(_filterConstants["default"].timeRange.key, true);
        remove();
      };

      (0, _useBeforeUnload.useBeforeUnload)(clean);
      (0, _react.useEffect)(function () {
        client.get('/time-ranges').then(function (_ref) {
          var data = _ref.data;
          update({
            items: data.items
          });
        })["catch"](function () {
          dispatchFilterError(_filterConstants["default"].timeRange.key);
        });
        return clean; // eslint-disable-next-line react-hooks/exhaustive-deps
      }, []);
    };

    exports.useTimeRangeFetch = useTimeRangeFetch;
    //# sourceMappingURL=useTimeRangeFetch.es.js.map
  }
});
//# sourceMappingURL=useTimeRangeFetch.es.js.map