import _Select from "@alifd/next/es/select";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
var _excluded = ["onChange", "placeholder", "defaultUint", "value", "unit"];
import * as React from 'react';
var AutoCompleteSetter = /*#__PURE__*/function (_React$Component) {
  function AutoCompleteSetter() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      dataSource: []
    };
    return _this;
  }
  _inheritsLoose(AutoCompleteSetter, _React$Component);
  var _proto = AutoCompleteSetter.prototype;
  _proto.handleChange = function handleChange(value) {
    var _this$props = this.props,
      onChange = _this$props.onChange,
      _this$props$unit = _this$props.unit,
      unit = _this$props$unit === void 0 ? [] : _this$props$unit;
    var newData = [];
    if (!this.hasUnit(value)) {
      unit.forEach(function (u) {
        return newData.push(value + u);
      });
    }
    this.setState({
      dataSource: [].concat(newData)
    });
    onChange === null || onChange === void 0 ? void 0 : onChange(value);
  };
  _proto.hasUnit = function hasUnit(value) {
    var _this$props$unit2, _this$props$unit2$fin;
    return !!((_this$props$unit2 = this.props.unit) !== null && _this$props$unit2 !== void 0 && (_this$props$unit2$fin = _this$props$unit2.find) !== null && _this$props$unit2$fin !== void 0 && _this$props$unit2$fin.call(_this$props$unit2, function (u) {
      var _value$endsWith;
      return value === null || value === void 0 ? void 0 : (_value$endsWith = value.endsWith) === null || _value$endsWith === void 0 ? void 0 : _value$endsWith.call(value, u);
    }));
  };
  _proto.render = function render() {
    var _this2 = this;
    var _this$props2 = this.props,
      onChange = _this$props2.onChange,
      placeholder = _this$props2.placeholder,
      defaultUint = _this$props2.defaultUint,
      value = _this$props2.value,
      _this$props2$unit = _this$props2.unit,
      unit = _this$props2$unit === void 0 ? [] : _this$props2$unit,
      rest = _objectWithoutPropertiesLoose(_this$props2, _excluded);
    return /*#__PURE__*/React.createElement(_Select.AutoComplete, _extends({
      value: value,
      dataSource: [].concat(this.state.dataSource),
      onFocus: function onFocus(e) {
        return e.target.select();
      },
      filterLocal: false,
      onBlur: function onBlur(e) {
        var input = e.target;
        if (input.value) {
          if (defaultUint) {
            if (!_this2.hasUnit(input.value)) {
              onChange === null || onChange === void 0 ? void 0 : onChange(input.value + defaultUint);
            }
          }
        }
      },
      placeholder: placeholder || '请输入',
      onChange: function onChange(value) {
        return _this2.handleChange(value);
      },
      style: {
        width: '100%'
      }
    }, rest));
  };
  return AutoCompleteSetter;
}(React.Component);
AutoCompleteSetter.displayName = 'AutoCompleteSetter';
export { AutoCompleteSetter as default };