Liferay.Loader.define("portal-workflow-metrics-web@2.0.46/js/shared/components/filter/util/filterUtil.es", ['module', 'exports', 'require', 'portal-workflow-metrics-web$path-to-regexp', '../../../util/util.es', '../../router/queryString.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.replaceHistory = exports.removeItem = exports.removeFilters = exports.reduceFilters = exports.mergeItemsArray = exports.getSelectedItemsQuery = exports.getSelectedItems = exports.getFilterValues = exports.getFilterResults = exports.getFiltersParam = exports.getFilterKeys = exports.getCapitalizedFilterKey = exports.buildFilterItems = exports.buildFilterItem = exports.asFilterObject = void 0;

    var _pathToRegexp = _interopRequireDefault(require("portal-workflow-metrics-web$path-to-regexp"));

    var _util = require("../../../util/util.es");

    var _queryString = require("../../router/queryString.es");

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

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

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

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    var asFilterObject = function asFilterObject(items, key, name, pinned) {
      return {
        items: items,
        key: key,
        name: name,
        pinned: pinned
      };
    };

    exports.asFilterObject = asFilterObject;

    var buildFilterItem = function buildFilterItem(data) {
      if (typeof data === 'string') {
        return {
          active: true,
          key: data
        };
      }

      return _objectSpread(_objectSpread({}, data), {}, {
        active: true
      });
    };

    exports.buildFilterItem = buildFilterItem;

    var buildFilterItems = function buildFilterItems(_ref) {
      var items = _ref.items,
          _ref$propertyKey = _ref.propertyKey,
          propertyKey = _ref$propertyKey === void 0 ? 'key' : _ref$propertyKey,
          selectedKeys = _ref.selectedKeys;
      return items.map(function (item, index) {
        var key = String(item[propertyKey]);
        return _objectSpread(_objectSpread({}, item), {}, {
          active: selectedKeys && selectedKeys.includes(key),
          dividerAfter: item.dividerAfter && !!items[index + 1],
          key: key
        });
      });
    };

    exports.buildFilterItems = buildFilterItems;

    var getFilterKeys = function getFilterKeys() {
      var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      return items.map(function (_ref2) {
        var key = _ref2.key;
        return key;
      });
    };

    exports.getFilterKeys = getFilterKeys;

    var getFiltersParam = function getFiltersParam(queryString) {
      var queryParams = (0, _queryString.parse)(queryString);
      return queryParams.filters || {};
    };

    exports.getFiltersParam = getFiltersParam;

    var getCapitalizedFilterKey = function getCapitalizedFilterKey(prefix, key) {
      return prefix ? "".concat(prefix).concat((0, _util.capitalize)(key)) : key;
    };

    exports.getCapitalizedFilterKey = getCapitalizedFilterKey;

    var getFilterResults = function getFilterResults(prefixedKeys, pinnedValues, titles, values) {
      var filterResults = [];
      prefixedKeys.forEach(function (prefixedKey, index) {
        if (values[prefixedKey]) {
          filterResults.push(asFilterObject(values[prefixedKey], prefixedKey, titles[index], pinnedValues[index]));
        }
      });
      return filterResults;
    };

    exports.getFilterResults = getFilterResults;

    var getFilterValues = function getFilterValues(filterState) {
      var filterValues = {};
      Object.keys(filterState).forEach(function (key) {
        if (filterState[key]) {
          filterValues[key] = getFilterKeys(filterState[key]);
        }
      });
      return filterValues;
    };

    exports.getFilterValues = getFilterValues;

    var getSelectedItemsQuery = function getSelectedItemsQuery(items, key, queryString) {
      var queryParams = (0, _queryString.parse)(queryString);
      var filtersParam = queryParams.filters || {};
      queryParams.filters = _objectSpread(_objectSpread({}, filtersParam), {}, _defineProperty({}, key, items.filter(function (item) {
        return item.active;
      }).map(function (item) {
        return item.key;
      })));
      return (0, _queryString.stringify)(queryParams);
    };

    exports.getSelectedItemsQuery = getSelectedItemsQuery;

    var getSelectedItems = function getSelectedItems(filterResults) {
      return filterResults.filter(function (filter) {
        filter.items = filter.items ? filter.items.filter(function (item) {
          return item.active;
        }) : [];
        return filter.items.length > 0;
      });
    };

    exports.getSelectedItems = getSelectedItems;

    var mergeItemsArray = function mergeItemsArray() {
      var baseItems = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

      for (var _len = arguments.length, items = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        items[_key - 1] = arguments[_key];
      }

      items = items.filter(function (value) {
        return value !== undefined && value !== null;
      });
      return baseItems.concat.apply(baseItems, _toConsumableArray(items));
    };

    exports.mergeItemsArray = mergeItemsArray;

    var reduceFilters = function reduceFilters(filterItems, paramKey) {
      return filterItems.reduce(function (acc, cur) {
        return "&".concat(paramKey, "=").concat(cur.key).concat(acc);
      }, '');
    };

    exports.reduceFilters = reduceFilters;

    var removeFilters = function removeFilters(queryString) {
      var queryParams = (0, _queryString.parse)(queryString);
      queryParams.filters = null;
      queryParams.search = null;
      return (0, _queryString.stringify)(queryParams);
    };

    exports.removeFilters = removeFilters;

    var removeItem = function removeItem(filterKey, itemToRemove, queryString) {
      var queryParams = (0, _queryString.parse)(queryString);
      var filtersParam = queryParams.filters || {};
      var filterValues = filtersParam[filterKey] || [];
      filtersParam[filterKey] = filterValues.filter(function (filterValue) {
        return filterValue !== itemToRemove.key;
      });
      queryParams.filters = filtersParam;
      return (0, _queryString.stringify)(queryParams);
    };

    exports.removeItem = removeItem;

    var replaceHistory = function replaceHistory(filterQuery, routerProps) {
      var history = routerProps.history,
          search = routerProps.location.search,
          _routerProps$match = routerProps.match,
          params = _routerProps$match.params,
          path = _routerProps$match.path;

      var pathname = _pathToRegexp.default.compile(path)(_objectSpread(_objectSpread({}, params), {}, {
        page: 1
      }));

      if (filterQuery !== search) {
        history.replace({
          pathname: pathname,
          search: filterQuery
        });
      }
    };

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