Liferay.Loader.define("app-builder-workflow-web@1.0.42/js/components/autocomplete/Autocomplete.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/autocomplete', '@frontend-taglib-clay$clayui/form', '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 = Autocomplete;

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

    var _form = require("@frontend-taglib-clay$clayui/form");

    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 _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 Autocomplete(_ref) {
      var children = _ref.children,
          _ref$defaultValue = _ref.defaultValue,
          defaultValue = _ref$defaultValue === void 0 ? '' : _ref$defaultValue,
          disabled = _ref.disabled,
          _ref$id = _ref.id,
          id = _ref$id === void 0 ? '' : _ref$id,
          items = _ref.items,
          onChange = _ref.onChange,
          onSelect = _ref.onSelect,
          _ref$placeholder = _ref.placeholder,
          placeholder = _ref$placeholder === void 0 ? Liferay.Language.get('select-or-type-an-option') : _ref$placeholder;

      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)(defaultValue),
          _useState8 = _slicedToArray(_useState7, 2),
          value = _useState8[0],
          setValue = _useState8[1];

      var _useState9 = (0, _react.useState)(false),
          _useState10 = _slicedToArray(_useState9, 2),
          selected = _useState10[0],
          setSelected = _useState10[1];

      var handleChange = (0, _react.useCallback)(function (_ref2) {
        var value = _ref2.target.value;

        if (selected) {
          onSelect();
          setSelected(false);
        }

        setDropDownVisible(true);
        setValue(value); // eslint-disable-next-line react-hooks/exhaustive-deps
      }, // eslint-disable-next-line react-hooks/exhaustive-deps
      [onSelect, selected]);
      var handleSelect = (0, _react.useCallback)(function (item) {
        onSelect(item);
        setActiveItem(-1);
        setDropDownVisible(false);
        setSelected(true);
        setValue(item.name);
      }, [onSelect]);
      var onBlur = (0, _react.useCallback)(function () {
        setDropDownVisible(false);
        setActiveItem(-1);

        if (!selected) {
          setValue('');
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [selected]);

      (0, _react.useEffect)(function () {
        if (disabled) {
          setValue('');
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [disabled]);
      (0, _react.useEffect)(function () {
        setFilteredItems(items);
      }, [items]);
      (0, _react.useEffect)(function () {
        if (!onChange) {
          var match = new RegExp(value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
          setFilteredItems(items ? items.filter(function (item) {
            return item.name.match(match);
          }) : []);
        } else {
          onChange(value);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [value]);
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_autocomplete.default, null, /*#__PURE__*/_react.default.createElement(_form.ClayInput.Group, null, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, null, /*#__PURE__*/_react.default.createElement(_autocomplete.default.Input, {
          className: "form-control ".concat(children ? 'input-group-inset input-group-inset-after' : ''),
          disabled: disabled,
          onBlur: onBlur,
          onChange: handleChange,
          onFocus: function onFocus() {
            setDropDownVisible(true);
          },
          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) {
              handleSelect(item);
            } else if (key === 'Tab') {
              onBlur();
            }
          },
          placeholder: placeholder,
          value: defaultValue || value
        }), children)), /*#__PURE__*/_react.default.createElement(_AutocompleteDropDown.default, {
          active: dropDownVisible,
          activeItem: activeItem,
          id: id,
          items: filteredItems,
          match: value,
          onSelect: handleSelect,
          setActiveItem: setActiveItem
        })))
      );
    }
    //# sourceMappingURL=Autocomplete.es.js.map
  }
});
//# sourceMappingURL=Autocomplete.es.js.map