var _excluded = ["canInput", "multiple", "dataSource", "onSearch", "onChange", "width", "style", "allowClear", "disabled", "value"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { Component } from 'react';
import { Select, Tag } from 'antd';
// import PropTypes from 'prop-types';
import { get } from 'lodash';
var CFSelect = /*#__PURE__*/function (_Component) {
  _inherits(CFSelect, _Component);
  var _super = _createSuper(CFSelect);
  function CFSelect() {
    var _this;
    _classCallCheck(this, CFSelect);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this), "handleSearch", function (keyword) {
      _this.searchContent = keyword;
      _this.props.onSearch(keyword);
    });
    _defineProperty(_assertThisInitialized(_this), "handleAutoCompleteChange", function (nextValue) {
      var _this$props, _this$props2;
      for (var _len2 = arguments.length, others = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
        others[_key2 - 1] = arguments[_key2];
      }
      (_this$props = _this.props).onChange.apply(_this$props, [nextValue].concat(others));
      (_this$props2 = _this.props).onSearch.apply(_this$props2, [nextValue].concat(others));
    });
    _defineProperty(_assertThisInitialized(_this), "handleChange", function (nextValue) {
      var _this$props3;
      _this.searchContent = null;
      for (var _len3 = arguments.length, others = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
        others[_key3 - 1] = arguments[_key3];
      }
      (_this$props3 = _this.props).onChange.apply(_this$props3, [nextValue].concat(others));
    });
    return _this;
  }
  _createClass(CFSelect, [{
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
        canInput = _this$props4.canInput,
        multiple = _this$props4.multiple,
        dataSource = _this$props4.dataSource,
        onSearch = _this$props4.onSearch,
        onChange = _this$props4.onChange,
        width = _this$props4.width,
        style = _this$props4.style,
        _this$props4$allowCle = _this$props4.allowClear,
        allowClear = _this$props4$allowCle === void 0 ? true : _this$props4$allowCle,
        _this$props4$disabled = _this$props4.disabled,
        disabled = _this$props4$disabled === void 0 ? false : _this$props4$disabled,
        value = _this$props4.value,
        others = _objectWithoutProperties(_this$props4, _excluded);
      var props = {
        mode: multiple ? 'multiple' : 'single',
        // // 如果选项数量超过 100，默认打开虚拟滚动
        // useVirtual: this.props.dataSource.length > 100,
        options: dataSource,
        value: value,
        disabled: disabled,
        onSearch: this.handleSearch,
        onChange: this.handleChange,
        style: _objectSpread({
          width: width
        }, style || {})
      };
      var SelectCom = Select;
      // onSearch 只能跟着 showSearch 一起用
      if (!props.showSearch) {
        delete props.onSearch;
      }
      if (multiple) {
        var searchContent = this.searchContent;
        // 可输入时，需要显式地将当前值添加到 dataSource 最前面。
        if (canInput && searchContent) {
          if (get(dataSource, [0, 'label']) !== searchContent) {
            props.options = [{
              label: searchContent,
              value: Date.now()
            }].concat(_toConsumableArray(dataSource));
          }
        }
        if (disabled) {
          var values = value === null || value === void 0 ? void 0 : value.map(function (item) {
            return dataSource === null || dataSource === void 0 ? void 0 : dataSource.find(function (val) {
              return val.value === item;
            });
          });
          return /*#__PURE__*/React.createElement(React.Fragment, null, values ? values === null || values === void 0 ? void 0 : values.map(function (val) {
            return /*#__PURE__*/React.createElement(Tag, null, val === null || val === void 0 ? void 0 : val.label);
          }) : /*#__PURE__*/React.createElement("span", null, "-"));
        }
      } else if (canInput) {
        // 单选，且可输入，使用 AutoComplete
        SelectCom = Select.AutoComplete;
        // AutoComplete 没有 onSearch，只有 onChange，因此需要在 onChange 时同时触发 onSearch
        props.onChange = this.handleAutoCompleteChange;
        delete props.onSearch;
        delete others.showSearch;
        // AutoComplete 默认使用 value，则选择下拉菜单时
        props.fillProps = 'label';
        // if (disabled) {
        //   const values = dataSource?.find(
        //     (val) => val.value?.toString() === value?.toString(),
        //   );
        //   return <span>{values?.label}</span>;
        // }
      }
      return /*#__PURE__*/React.createElement(SelectCom, _extends({}, others, props, {
        placeholder: "\u8BF7\u9009\u62E9",
        allowClear: allowClear
      }));
    }
  }]);
  return CFSelect;
}(Component);
// static propTypes = {
//   dataSource: PropTypes.arrayOf(PropTypes.object),
//   showSearch: PropTypes.bool,
//   canInput: PropTypes.bool,
//   multiple: PropTypes.bool,
//   onChange: PropTypes.func,
//   onSearch: PropTypes.func,
//   width: PropTypes.string,
// }
_defineProperty(CFSelect, "defaultProps", {
  dataSource: [],
  showSearch: false,
  canInput: false,
  multiple: false,
  onChange: function onChange() {},
  onSearch: function onSearch() {},
  width: '100%'
});
export { CFSelect as default };