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

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

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

    var _useFiltersConstants2 = require("../components/filter/hooks/useFiltersConstants.es");

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

    var _useRouterParams2 = require("./useRouterParams.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 useFilter = function useFilter(_ref) {
      var _ref$filterKeys = _ref.filterKeys,
          filterKeys = _ref$filterKeys === void 0 ? [] : _ref$filterKeys,
          _ref$prefixKeys = _ref.prefixKeys,
          prefixKeys = _ref$prefixKeys === void 0 ? [''] : _ref$prefixKeys,
          withoutRouteParams = _ref.withoutRouteParams;

      var _useContext = (0, _react.useContext)(_FilterContext.FilterContext),
          dispatch = _useContext.dispatch,
          dispatchFilter = _useContext.dispatchFilter,
          filterState = _useContext.filterState,
          filterValues = _useContext.filterValues;

      var _useRouterParams = (0, _useRouterParams2.useRouterParams)(),
          filters = _useRouterParams.filters;

      var _useFiltersConstants = (0, _useFiltersConstants2.useFiltersConstants)(filterKeys),
          keys = _useFiltersConstants.keys,
          pinnedValues = _useFiltersConstants.pinnedValues,
          titles = _useFiltersConstants.titles;

      var prefixedKeys = (0, _react.useMemo)(function () {
        var newKeys = [];
        keys.forEach(function (key) {
          return prefixKeys.forEach(function (prefix) {
            newKeys.push((0, _filterUtil.getCapitalizedFilterKey)(prefix, key));
          });
        });
        return newKeys;
      }, [keys, prefixKeys]);
      var filterResults = (0, _react.useMemo)(function () {
        return (0, _filterUtil.getFilterResults)(prefixedKeys, pinnedValues, titles, filterState);
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [filterState, prefixedKeys]);
      var hasFilterError = (0, _react.useCallback)(function (filterKey) {
        var _filterState$errors = filterState.errors,
            errors = _filterState$errors === void 0 ? [] : _filterState$errors;
        return errors.includes(filterKey);
      }, [filterState]);
      var filtersError = (0, _react.useMemo)(function () {
        return filterKeys.map(hasFilterError).reduce(function (current, next) {
          return current || next;
        }, false);
      }, [filterKeys, hasFilterError]);
      var selectedFilters = (0, _react.useMemo)(function () {
        return (0, _filterUtil.getSelectedItems)(filterResults);
      }, [filterResults]);
      return {
        dispatch: dispatch,
        dispatchFilter: dispatchFilter,
        filterState: filterState,
        filterValues: withoutRouteParams ? filterValues : filters,
        filtersError: filtersError,
        hasFilterError: hasFilterError,
        prefixedKeys: prefixedKeys,
        selectedFilters: selectedFilters
      };
    };

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