Liferay.Loader.define("app-builder-workflow-web@1.0.40/js/components/autocomplete/AutocompleteMultiSelect.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/autocomplete', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/tooltip', 'app-builder-web/js/utils/clickOutside.es', 'frontend-js-react-web$react', './AutocompleteDropDown.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 _autocomplete = _interopRequireDefault(require("@frontend-taglib-clay$clayui/autocomplete"));

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

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

    var _tooltip = require("@frontend-taglib-clay$clayui/tooltip");

    var _clickOutside = _interopRequireDefault(require("app-builder-web/js/utils/clickOutside.es"));

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

    var _AutocompleteDropDown = _interopRequireDefault(require("./AutocompleteDropDown.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 _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;
    }

    function AutocompleteMultiSelect(_ref) {
      var emptyMessage = _ref.emptyMessage,
          emptyResultMessage = _ref.emptyResultMessage,
          _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)(-1),
          _useState2 = _slicedToArray(_useState, 2),
          activeItem = _useState2[0],
          setActiveItem = _useState2[1];

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          dropDownVisible = _useState4[0],
          setDropDownVisible = _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 inputRef = (0, _react.useRef)();
      var wrapperRef = (0, _react.useRef)();
      var className = "".concat(highlighted ? 'is-active' : '', " align-items-start form-control form-control-tag-group multi-select-wrapper");

      var onBlur = function onBlur() {
        setActiveItem(-1);
        setDropDownVisible(false);
        setHighlighted(false);
        setSearch('');
      };

      var onSelect = function onSelect(item) {
        var newSelectedItems = [].concat(_toConsumableArray(selectedItems), [item]);
        setSearch('');
        onChange(newSelectedItems);

        if (inputRef.current) {
          inputRef.current.focus();
        }
      };

      var _onRemove = function onRemove(id) {
        var newSelectedItems = selectedItems.filter(function (item) {
          return id !== item['id'];
        });
        onChange(newSelectedItems);
      };

      (0, _react.useEffect)(function () {
        var listener = function listener(event) {
          var dropdown = document.getElementById("dropDownList".concat(id));

          if ((0, _clickOutside.default)(event.target, dropdown === null || dropdown === void 0 ? void 0 : dropdown.parentNode, wrapperRef.current)) {
            onBlur();
          }
        };

        document.addEventListener('mousedown', listener);
        return function () {
          document.removeEventListener('mousedown', listener);
        };
      }, [id, wrapperRef]);
      (0, _react.useEffect)(function () {
        var term = search.toLowerCase().trim();
        setFilteredItems(items.filter(function (item) {
          return !selectedItems.find(function (selectedItem) {
            return item['id'] === selectedItem['id'];
          });
        }).filter(function (_ref4) {
          var name = _ref4.name;
          return name.toLowerCase().indexOf(term) > -1;
        }));
      }, [items, search, selectedItems]);
      return (/*#__PURE__*/_react.default.createElement(_autocomplete.default, {
          className: className,
          ref: wrapperRef
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "col-11 d-flex flex-wrap p-0"
        }, selectedItems.map(function (_ref5, index) {
          var id = _ref5.id,
              name = _ref5.name;
          return (/*#__PURE__*/_react.default.createElement(AutocompleteMultiSelect.Item, {
              key: index,
              name: name,
              onRemove: function onRemove() {
                return _onRemove(id);
              }
            })
          );
        }), /*#__PURE__*/_react.default.createElement("input", {
          className: "form-control-inset",
          onChange: function onChange(_ref6) {
            var target = _ref6.target;
            return setSearch(target.value);
          },
          onFocus: function onFocus(_ref2) {
            var target = _ref2.target;
            setDropDownVisible(true);
            setHighlighted(true);
            setSearch('');
            target.value = '';
          },
          onKeyDown: function onKeyDown(_ref3) {
            var key = _ref3.key;
            var item = filteredItems[activeItem];

            var updateIndex = function updateIndex(index) {
              setActiveItem(index);
              var element = document.querySelector("#dropDownList".concat(id, " > li:nth-child(").concat(index, ")"));

              if (typeof (element === null || element === void 0 ? void 0 : element.scrollIntoView) === 'function') {
                element.scrollIntoView();
              }
            };

            if (key === 'ArrowDown' && activeItem < filteredItems.length - 1) {
              updateIndex(activeItem + 1);
            } else if (key === 'ArrowUp' && activeItem > 0) {
              updateIndex(activeItem - 1);
            } else if (key === 'Enter' && item) {
              onSelect(item);
            } else if (key === 'Tab') {
              onBlur();
            }
          },
          placeholder: !selectedItems.length ? placeholder : '',
          ref: inputRef,
          style: selectedItems && {
            width: 0
          },
          type: "text",
          value: search
        })), selectedItems.length > 0 && /*#__PURE__*/_react.default.createElement(_tooltip.ClayTooltipProvider, null, /*#__PURE__*/_react.default.createElement(_button.default, {
          borderless: true,
          className: "ml-2 pl-0 pr-1 py-0",
          displayType: "light",
          onClick: function onClick() {
            return onChange([]);
          },
          style: {
            position: 'absolute',
            right: '1rem'
          }
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          className: "text-secondary tooltip-icon",
          "data-tooltip-align": "top",
          "data-tooltip-delay": "0",
          symbol: "times-circle",
          title: Liferay.Language.get('clear-all')
        }))), /*#__PURE__*/_react.default.createElement(_AutocompleteDropDown.default, {
          active: dropDownVisible,
          activeItem: activeItem,
          emptyMessage: emptyMessage,
          emptyResultMessage: emptyResultMessage,
          id: id,
          items: filteredItems,
          match: search,
          onSelect: onSelect,
          setActiveItem: setActiveItem,
          setDropDownVisible: setDropDownVisible
        }))
      );
    }

    AutocompleteMultiSelect.Item = function (_ref7) {
      var name = _ref7.name,
          onRemove = _ref7.onRemove;
      return (/*#__PURE__*/_react.default.createElement("span", {
          className: "label label-dismissible label-secondary"
        }, /*#__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"
        }))))
      );
    };

    var _default = AutocompleteMultiSelect;
    exports.default = _default;
    //# sourceMappingURL=AutocompleteMultiSelect.es.js.map
  }
});
//# sourceMappingURL=AutocompleteMultiSelect.es.js.map