Liferay.Loader.define("portal-workflow-metrics-web@2.0.59/js/shared/components/autocomplete/AutocompleteMultiSelect.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/autocomplete', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/layout', 'frontend-js-react-web$react', '../filter/util/filterEvents.es', './Autocomplete.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.AutocompleteMultiSelect = void 0;

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

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

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

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

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

    var _Autocomplete = require("./Autocomplete.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 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;
    }

    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 _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 AutocompleteMultiSelect = function AutocompleteMultiSelect(_ref) {
      var _ref$fieldId = _ref.fieldId,
          fieldId = _ref$fieldId === void 0 ? 'id' : _ref$fieldId,
          _ref$fieldName = _ref.fieldName,
          fieldName = _ref$fieldName === void 0 ? 'name' : _ref$fieldName,
          _ref$id = _ref.id,
          id = _ref$id === void 0 ? '' : _ref$id,
          items = _ref.items,
          onChange = _ref.onChange,
          _ref$placeholder = _ref.placeholder,
          placeholder = _ref$placeholder === void 0 ? Liferay.Language.get('select-or-type-an-option') : _ref$placeholder,
          _ref$selectedItems = _ref.selectedItems,
          selectedItems = _ref$selectedItems === void 0 ? [] : _ref$selectedItems;

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

      var _useState3 = (0, _react.useState)(-1),
          _useState4 = _slicedToArray(_useState3, 2),
          currentIndex = _useState4[0],
          setCurrentIndex = _useState4[1];

      var _useState5 = (0, _react.useState)(items),
          _useState6 = _slicedToArray(_useState5, 2),
          filteredItems = _useState6[0],
          setFilteredItems = _useState6[1];

      var _useState7 = (0, _react.useState)(false),
          _useState8 = _slicedToArray(_useState7, 2),
          highlighted = _useState8[0],
          setHighlighted = _useState8[1];

      var _useState9 = (0, _react.useState)(''),
          _useState10 = _slicedToArray(_useState9, 2),
          search = _useState10[0],
          setSearch = _useState10[1];

      var wrapperRef = (0, _react.useRef)();
      var className = "".concat(highlighted ? 'is-active' : '', " align-items-start form-control form-control-tag-group multi-select-wrapper");

      var handleBlur = function handleBlur() {
        setActive(false);
        setCurrentIndex(-1);
        setHighlighted(false);
        setSearch('');
      };

      var handleChange = (0, _react.useCallback)(function (newSelectedItems) {
        if (onChange) {
          onChange(newSelectedItems);
        }
      }, [onChange]);

      var handleFocus = function handleFocus() {
        setActive(true);
        setHighlighted(true);
      };

      var handleSelect = (0, _react.useCallback)(function (item) {
        var newSelectedItems = [].concat(_toConsumableArray(selectedItems), [item]);
        handleChange(newSelectedItems);
      }, [handleChange, selectedItems]);
      var handleKeyDown = (0, _react.useCallback)(function (_ref2) {
        var keyCode = _ref2.keyCode;

        var item = filteredItems[currentIndex];

        if (keyCode === 40 && currentIndex < filteredItems.length - 1) {
          setCurrentIndex(currentIndex + 1);
        } else if (keyCode === 38 && currentIndex > 0) {
          setCurrentIndex(currentIndex - 1);
        } else if (keyCode === 13 && item) {
          handleSelect(item);
        } else if (keyCode === 9) {
          handleBlur();
        }
      }, [currentIndex, filteredItems, handleSelect]);
      var handleRemove = (0, _react.useCallback)(function (id) {
        var newSelectedItems = selectedItems.filter(function (item) {
          return id !== item[fieldId];
        });
        handleChange(newSelectedItems);
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [fieldId, selectedItems]);
      (0, _react.useEffect)(function () {
        var term = search.toLowerCase().trim();
        setFilteredItems(items.filter(function (item) {
          return !selectedItems.find(function (selectedItem) {
            return item[fieldId] === selectedItem[fieldId];
          });
        }).filter(function (_ref3) {
          var name = _ref3[fieldName];
          return name.toLowerCase().indexOf(term) > -1;
        }));
      }, [fieldId, fieldName, items, search, selectedItems]);
      (0, _react.useEffect)(function () {
        var listener = (0, _filterEvents.handleClickOutside)(function (event) {
          var listenerCallback = (0, _filterEvents.handleClickOutside)(handleBlur, document.getElementById("dropDownList".concat(id)));
          listenerCallback(event);
        }, wrapperRef.current);
        (0, _filterEvents.addClickOutsideListener)(listener);
        return function () {
          (0, _filterEvents.removeClickOutsideListener)(listener);
        };
      }, [id, wrapperRef]);
      return (/*#__PURE__*/_react.default.createElement(_autocomplete.default, null, /*#__PURE__*/_react.default.createElement("div", {
          className: className,
          ref: wrapperRef
        }, /*#__PURE__*/_react.default.createElement(_layout.default.Col, {
          className: "d-flex flex-wrap p-0",
          size: "11"
        }, selectedItems.map(function (_ref4, index) {
          var id = _ref4[fieldId],
              name = _ref4[fieldName];
          return (/*#__PURE__*/_react.default.createElement(AutocompleteMultiSelect.Item, {
              key: index,
              name: name,
              onRemove: function onRemove() {
                return handleRemove(id);
              }
            })
          );
        }), /*#__PURE__*/_react.default.createElement("input", {
          className: "form-control-inset",
          onChange: function onChange(_ref5) {
            var target = _ref5.target;
            return setSearch(target.value);
          },
          onFocus: handleFocus,
          onKeyDown: handleKeyDown,
          placeholder: !selectedItems.length ? placeholder : '',
          type: "text"
        })), /*#__PURE__*/_react.default.createElement(_layout.default.Col, {
          className: "drop-icon mt-1 text-right",
          onClick: handleFocus,
          size: "1"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "caret-double"
        })), /*#__PURE__*/_react.default.createElement(_Autocomplete.Autocomplete.DropDown, {
          active: active,
          activeItem: currentIndex,
          id: id,
          items: filteredItems.map(function (item) {
            return _objectSpread(_objectSpread({}, item), {}, {
              name: item[fieldName]
            });
          }),
          match: search,
          onSelect: handleSelect,
          setActive: setActive,
          setActiveItem: setCurrentIndex
        })))
      );
    };

    exports.AutocompleteMultiSelect = AutocompleteMultiSelect;

    var Item = function Item(_ref6) {
      var key = _ref6.key,
          name = _ref6.name,
          onRemove = _ref6.onRemove;
      return (/*#__PURE__*/_react.default.createElement("span", {
          className: "label label-dismissible label-secondary",
          key: key
        }, /*#__PURE__*/_react.default.createElement("span", {
          className: "label-item label-item-expand"
        }, name), /*#__PURE__*/_react.default.createElement("span", {
          className: "label-item label-item-after"
        }, /*#__PURE__*/_react.default.createElement("button", {
          "aria-label": "Close",
          className: "close",
          onClick: onRemove,
          type: "button"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "times"
        }))))
      );
    };

    AutocompleteMultiSelect.Item = Item;
    //# sourceMappingURL=AutocompleteMultiSelect.es.js.map
  }
});
//# sourceMappingURL=AutocompleteMultiSelect.es.js.map