Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/shared/components/filter/Filter.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/icon', 'frontend-js-react-web$classnames', 'frontend-js-react-web$react', '../../hooks/useFilter.es', '../../hooks/useRouter.es', './FilterItem.es', './FilterSearch.es', './util/filterEvents.es', './util/filterUtil.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports["default"] = void 0;

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

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

    var _useFilter2 = require("../../hooks/useFilter.es");

    var _useRouter = require("../../hooks/useRouter.es");

    var _FilterItem = require("./FilterItem.es");

    var _FilterSearch = require("./FilterSearch.es");

    var _filterEvents = require("./util/filterEvents.es");

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

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { "default": obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj["default"] = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

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

    function _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

    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;
    }

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    var Filter = function Filter(_ref) {
      var _ref$buttonClassName = _ref.buttonClassName,
          buttonClassName = _ref$buttonClassName === void 0 ? 'btn-secondary btn-sm' : _ref$buttonClassName,
          children = _ref.children,
          defaultItem = _ref.defaultItem,
          disabled = _ref.disabled,
          elementClasses = _ref.elementClasses,
          filterKey = _ref.filterKey,
          _ref$hideControl = _ref.hideControl,
          hideControl = _ref$hideControl === void 0 ? false : _ref$hideControl,
          items = _ref.items,
          _ref$labelPropertyNam = _ref.labelPropertyName,
          labelPropertyName = _ref$labelPropertyNam === void 0 ? 'name' : _ref$labelPropertyNam,
          _ref$multiple = _ref.multiple,
          multiple = _ref$multiple === void 0 ? true : _ref$multiple,
          name = _ref.name,
          onClickFilter = _ref.onClickFilter,
          _ref$position = _ref.position,
          position = _ref$position === void 0 ? 'left' : _ref$position,
          _ref$prefixKey = _ref.prefixKey,
          prefixKey = _ref$prefixKey === void 0 ? '' : _ref$prefixKey,
          preventClick = _ref.preventClick,
          withoutRouteParams = _ref.withoutRouteParams,
          otherProps = _objectWithoutProperties(_ref, ["buttonClassName", "children", "defaultItem", "disabled", "elementClasses", "filterKey", "hideControl", "items", "labelPropertyName", "multiple", "name", "onClickFilter", "position", "prefixKey", "preventClick", "withoutRouteParams"]);

      var _useFilter = (0, _useFilter2.useFilter)({
        withoutRouteParams: withoutRouteParams
      }),
          dispatchFilter = _useFilter.dispatchFilter;

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          expanded = _useState2[0],
          setExpanded = _useState2[1];

      var _useState3 = (0, _react.useState)(''),
          _useState4 = _slicedToArray(_useState3, 2),
          searchTerm = _useState4[0],
          setSearchTerm = _useState4[1];

      var _useState5 = (0, _react.useState)(false),
          _useState6 = _slicedToArray(_useState5, 2),
          changed = _useState6[0],
          setChanged = _useState6[1];

      var prefixedFilterKey = (0, _filterUtil.getCapitalizedFilterKey)(prefixKey, filterKey);
      var routerProps = (0, _useRouter.useRouter)();
      var wrapperRef = (0, _react.useRef)();
      var classes = (0, _react.useMemo)(function () {
        return {
          children: (0, _classnames["default"])('custom dropdown-menu', children && 'show', position && "dropdown-menu-".concat(position)),
          custom: (0, _classnames["default"])('btn dropdown-toggle nav-link', buttonClassName),
          dropdown: (0, _classnames["default"])('dropdown nav-item', elementClasses),
          menu: (0, _classnames["default"])('dropdown-menu', expanded && 'show', position && "dropdown-menu-".concat(position))
        };
      }, [buttonClassName, children, elementClasses, expanded, position]);
      var filteredItems = (0, _react.useMemo)(function () {
        return searchTerm ? items.filter(function (item) {
          return item[labelPropertyName].toLowerCase().includes(searchTerm.toLowerCase());
        }) : items;
      }, [items, labelPropertyName, searchTerm]);
      var applyFilterChanges = (0, _react.useCallback)(function () {
        if (!withoutRouteParams) {
          var query = (0, _filterUtil.getSelectedItemsQuery)(items, prefixedFilterKey, routerProps.location.search);
          (0, _filterUtil.replaceHistory)(query, routerProps);
        } else {
          dispatchFilter(prefixedFilterKey, getSelectedItems(items));
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [items, routerProps]);

      var closeDropdown = function closeDropdown() {
        setExpanded(false);
        setSearchTerm('');
      };

      var getSelectedItems = function getSelectedItems(items) {
        return items.filter(function (item) {
          return item.active;
        });
      };

      var onClick = function onClick(item) {
        return onClickFilter ? onClickFilter(item) : true;
      };

      var onSelect = (0, _react.useCallback)(function (item) {
        if (!preventClick) {
          if (!multiple) {
            items.forEach(function (item) {
              item.active = false;
            });
          }

          item.active = !item.active;

          if (!multiple) {
            applyFilterChanges();
            closeDropdown();
          } else {
            setChanged(true);
          }
        } else {
          onClick(item);
        }
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [applyFilterChanges, items]);
      var selectDefaultItem = (0, _react.useCallback)(function () {
        if (defaultItem && !multiple) {
          var selectedItems = getSelectedItems(items);

          if (!selectedItems.length) {
            var index = items.findIndex(function (item) {
              return item.key === defaultItem.key;
            });
            items[index].active = true;

            if (!preventClick) {
              applyFilterChanges();
            } else {
              onClick(items[index]);
            }
          }
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [applyFilterChanges, defaultItem, items]);
      (0, _react.useEffect)(function () {
        selectDefaultItem(); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [defaultItem]);
      (0, _react.useEffect)(function () {
        var callback = (0, _filterEvents.handleClickOutside)(function () {
          if (expanded) {
            closeDropdown();

            if (changed) {
              setChanged(false);
              applyFilterChanges();
            }
          }
        }, wrapperRef.current);
        (0, _filterEvents.addClickOutsideListener)(callback);
        return function () {
          (0, _filterEvents.removeClickOutsideListener)(callback);
        }; // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [applyFilterChanges, expanded, changed]);
      return _react["default"].createElement("li", _extends({
        className: classes.dropdown,
        ref: wrapperRef
      }, otherProps), _react["default"].createElement("button", {
        className: classes.custom,
        disabled: disabled,
        onClick: function onClick() {
          setExpanded(!expanded);
        }
      }, _react["default"].createElement("span", {
        className: "mr-2 navbar-text-truncate"
      }, name), _react["default"].createElement(_icon["default"], {
        symbol: "caret-bottom"
      })), _react["default"].createElement("div", {
        className: classes.menu,
        role: "menu"
      }, _react["default"].createElement(_FilterSearch.FilterSearch, {
        filteredItems: filteredItems,
        onChange: function onChange(_ref2) {
          var target = _ref2.target;
          setSearchTerm(target.value);
        },
        searchTerm: searchTerm,
        totalCount: items.length
      }, _react["default"].createElement("ul", {
        className: "list-unstyled"
      }, filteredItems.map(function (item, index) {
        return _react["default"].createElement(_FilterItem.FilterItem, _extends({}, item, {
          hideControl: hideControl,
          key: index,
          labelPropertyName: labelPropertyName,
          multiple: multiple,
          onClick: function onClick() {
            return onSelect(item);
          },
          preventClick: preventClick
        }));
      })))), _react["default"].createElement("div", {
        className: classes.children
      }, children));
    };

    var _default = Filter;
    exports["default"] = _default;
    //# sourceMappingURL=Filter.es.js.map
  }
});
//# sourceMappingURL=Filter.es.js.map