import _typeof from "@babel/runtime/helpers/typeof";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Children, cloneElement, Component, isValidElement } from 'react';
import classNames from 'classnames';
import Select from '../select';
import Input from '../input';
import InputElement from './InputElement';
import { OptGroup, Option } from '../rc-components/select';
import { Size } from '../_util/enum';
import { SelectMode } from '../select/enum';
import ConfigContext from '../config-provider/ConfigContext';

function isSelectOptionOrSelectOptGroup(child) {
  return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
}

var AutoComplete = /*#__PURE__*/function (_Component) {
  _inherits(AutoComplete, _Component);

  var _super = _createSuper(AutoComplete);

  function AutoComplete() {
    var _this;

    _classCallCheck(this, AutoComplete);

    _this = _super.apply(this, arguments);

    _this.getInputElement = function () {
      var _this$props = _this.props,
          children = _this$props.children,
          inputProps = _this$props.inputProps;
      var element = children && /*#__PURE__*/isValidElement(children) && children.type !== Option ? /*#__PURE__*/cloneElement(Children.only(children), {
        labelLayout: 'none'
      }) : /*#__PURE__*/React.createElement(Input, _extends({}, inputProps, {
        border: false
      }));

      var elementProps = _objectSpread({}, element.props);

      delete elementProps.children;
      return /*#__PURE__*/React.createElement(InputElement, _extends({}, elementProps), element);
    };

    _this.saveSelect = function (node) {
      _this.select = node;
    };

    return _this;
  }

  _createClass(AutoComplete, [{
    key: "focus",
    value: function focus() {
      this.select.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.select.blur();
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          size = _this$props2.size,
          _this$props2$classNam = _this$props2.className,
          className = _this$props2$classNam === void 0 ? '' : _this$props2$classNam,
          notFoundContent = _this$props2.notFoundContent,
          customizePrefixCls = _this$props2.prefixCls,
          optionLabelProp = _this$props2.optionLabelProp,
          dataSource = _this$props2.dataSource,
          children = _this$props2.children;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('select', customizePrefixCls);
      var cls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === Size.large), _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === Size.small), _defineProperty(_classNames, className, !!className), _defineProperty(_classNames, "".concat(prefixCls, "-show-search"), true), _defineProperty(_classNames, "".concat(prefixCls, "-auto-complete"), true), _classNames));
      var options;
      var childArray = Children.toArray(children);

      if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) {
        options = children;
      } else {
        options = dataSource ? dataSource.map(function (item) {
          if ( /*#__PURE__*/isValidElement(item)) {
            return item;
          }

          switch (_typeof(item)) {
            case 'string':
              return /*#__PURE__*/React.createElement(Option, {
                key: item
              }, item);

            case 'object':
              return /*#__PURE__*/React.createElement(Option, {
                key: item.value
              }, item.text);

            default:
              throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.');
          }
        }) : [];
      }

      return /*#__PURE__*/React.createElement(Select, _extends({}, this.props, {
        prefixCls: prefixCls,
        className: cls,
        mode: SelectMode.combobox,
        optionLabelProp: optionLabelProp,
        getInputElement: this.getInputElement,
        notFoundContent: notFoundContent,
        ref: this.saveSelect
      }), options);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return AutoComplete;
}(Component);

export { AutoComplete as default };
AutoComplete.displayName = 'AutoComplete';
AutoComplete.Option = Option;
AutoComplete.OptGroup = OptGroup;
AutoComplete.defaultProps = {
  transitionName: 'slide-up',
  optionLabelProp: 'children',
  choiceTransitionName: 'zoom',
  showSearch: false,
  filterOption: false
};
//# sourceMappingURL=index.js.map
