import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useRef } from 'react';
import classNames from 'classnames';
import Select, { Option } from '../select/select';
import { ConfigContext } from '../config-provider/context';
import toArray from '../components/rc-util/Children/toArray';
var InnerSelect = Select;

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.forwardRef(function (props, ref) {
  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var autoCompleteRef = ref || useRef(null);
  var autoCompleteCls = getPrefixClassName('select');

  var className = props.className,
      children = props.children,
      style = props.style,
      restProps = _objectWithoutProperties(props, ["className", "children", "style"]);

  var endCls = classNames("".concat(autoCompleteCls, "-auto-complete"), className);
  var childNodes = toArray(children); // ============================= Input =============================

  var customizeInput;

  if (childNodes.length === 1 && /*#__PURE__*/React.isValidElement(childNodes[0]) && !isSelectOptionOrSelectOptGroup(childNodes[0])) {
    var _childNodes = _slicedToArray(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 = _objectSpread({
    ref: autoCompleteRef,
    showSearch: true,
    showArrow: false,
    className: endCls,
    prefixCls: autoCompleteCls,
    style: style
  }, restProps);

  return /*#__PURE__*/React.createElement(InnerSelect, Object.assign({
    mode: "combobox"
  }, comProp, {
    getInputElement: getInputElement
  }), optionChildren);
});
AutoComplete.Option = Option;
AutoComplete.displayName = 'AutoComplete';
AutoComplete.defaultProps = {
  allowClear: false,
  autoFocus: false
};
export { Option };
export default AutoComplete;