"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "Option", {
  enumerable: true,
  get: function get() {
    return _select.Option;
  }
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _select = _interopRequireWildcard(require("../select/select"));

var _context = require("../config-provider/context");

var _toArray = _interopRequireDefault(require("../components/rc-util/Children/toArray"));

var InnerSelect = _select.default;

function isSelectOptionOrSelectOptGroup(child) {
  var _child$type, _child$type2;

  return (child === null || child === void 0 ? void 0 : (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type.isSelectOption) || (child === null || child === void 0 ? void 0 : (_child$type2 = child.type) === null || _child$type2 === void 0 ? void 0 : _child$type2.isSelectOptGroup);
}

var AutoComplete = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var autoCompleteRef = ref || (0, _react.useRef)(null);
  var autoCompleteCls = getPrefixClassName('select');
  var className = props.className,
      children = props.children,
      style = props.style,
      restProps = (0, _objectWithoutProperties2.default)(props, ["className", "children", "style"]);
  var endCls = (0, _classnames.default)("".concat(autoCompleteCls, "-auto-complete"), className);
  var childNodes = (0, _toArray.default)(children); // ============================= Input =============================

  var customizeInput;

  if (childNodes.length === 1 && /*#__PURE__*/_react.default.isValidElement(childNodes[0]) && !isSelectOptionOrSelectOptGroup(childNodes[0])) {
    var _childNodes = (0, _slicedToArray2.default)(childNodes, 1);

    customizeInput = _childNodes[0];
  }

  var getInputElement = customizeInput ? function () {
    return customizeInput;
  } : undefined;
  var optionChildren;

  if (childNodes.length && isSelectOptionOrSelectOptGroup(childNodes[0])) {
    optionChildren = children;
  } else {
    optionChildren = [];
  }

  var comProp = (0, _objectSpread2.default)({
    ref: autoCompleteRef,
    showSearch: true,
    showArrow: false,
    className: endCls,
    prefixCls: autoCompleteCls,
    style: style
  }, restProps);
  return /*#__PURE__*/_react.default.createElement(InnerSelect, Object.assign({
    mode: "combobox"
  }, comProp, {
    getInputElement: getInputElement
  }), optionChildren);
});

AutoComplete.Option = _select.Option;
AutoComplete.displayName = 'AutoComplete';
AutoComplete.defaultProps = {
  allowClear: false,
  autoFocus: false
};
var _default = AutoComplete;
exports.default = _default;