Liferay.Loader.define("portal-workflow-metrics-web@2.0.42/js/shared/components/autocomplete/Autocomplete.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/autocomplete', '@frontend-taglib-clay$clayui/form', 'frontend-js-react-web$react', '../promises-resolver/PromisesResolver.es', './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.Autocomplete = void 0;

    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 _PromisesResolver = _interopRequireDefault(require("../promises-resolver/PromisesResolver.es"));

    var _AutocompleteDropDown = 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;
    }

    var Autocomplete = function Autocomplete(_ref) {
      var children = _ref.children,
          _ref$defaultValue = _ref.defaultValue,
          defaultValue = _ref$defaultValue === void 0 ? '' : _ref$defaultValue,
          disabled = _ref.disabled,
          items = _ref.items,
          onChange = _ref.onChange,
          onSelect = _ref.onSelect,
          _ref$placeholder = _ref.placeholder,
          placeholder = _ref$placeholder === void 0 ? '' : _ref$placeholder,
          _ref$promises = _ref.promises,
          promises = _ref$promises === void 0 ? [] : _ref$promises;

      var _useState = (0, _react.useState)(-1),
          _useState2 = _slicedToArray(_useState, 2),
          activeItem = _useState2[0],
          setActiveItem = _useState2[1];

      var _useState3 = (0, _react.useState)([]),
          _useState4 = _slicedToArray(_useState3, 2),
          dropDownItems = _useState4[0],
          setDropDownItems = _useState4[1];

      var _useState5 = (0, _react.useState)(defaultValue),
          _useState6 = _slicedToArray(_useState5, 2),
          value = _useState6[0],
          setValue = _useState6[1];

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

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

      var handleBlur = (0, _react.useCallback)(function () {
        setDropDownVisible(false);
        setActiveItem(-1);

        if (!selected) {
          setValue('');
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [selected]);
      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 handleKeyDown = (0, _react.useCallback)(function (_ref3) {
        var keyCode = _ref3.keyCode;
        var item = dropDownItems[activeItem];

        if (keyCode === 38 && activeItem > 0) {
          setActiveItem(activeItem - 1);
        } else if (keyCode === 40 && activeItem < dropDownItems.length - 1) {
          setActiveItem(activeItem + 1);
        } else if (keyCode === 13 && item) {
          handleSelect(item);
        }
      }, [activeItem, dropDownItems, handleSelect]);
      var handleSelect = (0, _react.useCallback)(function (item) {
        onSelect(item);
        setActiveItem(-1);
        setDropDownVisible(false);
        setSelected(true);
        setValue(item.name);
      }, [onSelect]);
      (0, _react.useEffect)(function () {
        setDropDownItems(items);
      }, [items]);
      (0, _react.useEffect)(function () {
        if (!onChange) {
          var regExpValue = formatRegExp(value);
          var match = new RegExp(regExpValue, 'gi');
          setDropDownItems(items ? items.filter(function (item) {
            return item.name.match(match);
          }) : []);
        } else {
          onChange(value);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [value]);
      (0, _react.useEffect)(function () {
        if (disabled) {
          setValue('');
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [disabled]);
      return _react["default"].createElement(_PromisesResolver["default"], {
        promises: promises
      }, _react["default"].createElement(_autocomplete["default"], null, _react["default"].createElement(_form.ClayInput.Group, null, _react["default"].createElement(_form.ClayInput.GroupItem, null, _react["default"].createElement(_autocomplete["default"].Input, {
        className: "form-control ".concat(children ? 'input-group-inset input-group-inset-after' : ''),
        "data-testid": "autocompleteInput",
        disabled: disabled,
        onBlur: handleBlur,
        onChange: handleChange,
        onFocus: function handleFocus() {
          setDropDownVisible(true);
        },
        onKeyDown: handleKeyDown,
        placeholder: placeholder,
        value: defaultValue || value
      }), children)), _react["default"].createElement(Autocomplete.DropDown, {
        active: dropDownVisible,
        activeItem: activeItem,
        items: dropDownItems,
        match: value,
        onSelect: handleSelect,
        setActiveItem: setActiveItem
      })));
    };

    exports.Autocomplete = Autocomplete;

    var formatRegExp = function formatRegExp(value) {
      return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    };

    Autocomplete.DropDown = _AutocompleteDropDown.DropDown;
    //# sourceMappingURL=Autocomplete.es.js.map
  }
});
//# sourceMappingURL=Autocomplete.es.js.map