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

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

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

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

    var _filterUtil = require("../util/filterUtil.es");

    var _useFilterState2 = require("./useFilterState.es");

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * The contents of this file are subject to the terms of the Liferay Enterprise
     * Subscription License ("License"). You may not use this file except in
     * compliance with the License. You can obtain a copy of the License by
     * contacting Liferay, Inc. See the License for the specific language governing
     * permissions and limitations under the License, including but not limited to
     * distribution rights of the Software.
     */
    var useFilterFetch = function useFilterFetch(_ref) {
      var filterKey = _ref.filterKey,
          _ref$labelPropertyNam = _ref.labelPropertyName,
          labelPropertyName = _ref$labelPropertyNam === void 0 ? 'label' : _ref$labelPropertyNam,
          prefixKey = _ref.prefixKey,
          _ref$requestBody = _ref.requestBody,
          data = _ref$requestBody === void 0 ? {} : _ref$requestBody,
          propertyKey = _ref.propertyKey,
          _ref$requestMethod = _ref.requestMethod,
          method = _ref$requestMethod === void 0 ? 'get' : _ref$requestMethod,
          _ref$requestParams = _ref.requestParams,
          params = _ref$requestParams === void 0 ? {} : _ref$requestParams,
          url = _ref.requestUrl,
          staticData = _ref.staticData,
          staticItems = _ref.staticItems,
          withoutRouteParams = _ref.withoutRouteParams;

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

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

      var _useFilterState = (0, _useFilterState2.useFilterState)((0, _filterUtil.getCapitalizedFilterKey)(prefixKey, filterKey), withoutRouteParams),
          items = _useFilterState.items,
          selectedItems = _useFilterState.selectedItems,
          selectedKeys = _useFilterState.selectedKeys,
          setItems = _useFilterState.setItems;

      var parseResponse = function parseResponse(_ref2) {
        var _ref2$data = _ref2.data,
            data = _ref2$data === void 0 ? {} : _ref2$data;
        data.items.sort(function (current, next) {
          var _current$labelPropert;

          return (_current$labelPropert = current[labelPropertyName]) === null || _current$labelPropert === void 0 ? void 0 : _current$labelPropert.localeCompare(next[labelPropertyName]);
        });
        var mergedItems = (0, _filterUtil.mergeItemsArray)(staticItems, data.items);
        var mappedItems = (0, _filterUtil.buildFilterItems)({
          items: mergedItems,
          propertyKey: propertyKey,
          selectedKeys: selectedKeys
        });
        setItems(mappedItems);
      };

      (0, _react.useEffect)(function () {
        dispatchFilterError(filterKey, true);

        if (staticData) {
          parseResponse({
            data: {
              items: staticData
            }
          });
        } else {
          client.request({
            data: data,
            method: method,
            params: params,
            url: url
          }).then(parseResponse).catch(function () {
            return dispatchFilterError(filterKey);
          });
        }
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      []);
      return {
        items: items,
        selectedItems: selectedItems
      };
    };

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