"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

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

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

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

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

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

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

var _omit = _interopRequireDefault(require("omit.js"));

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

var _lodash = require("lodash");

var _rcSelect = _interopRequireWildcard(require("rc-select"));

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

var _iconUtil = _interopRequireDefault(require("./utils/iconUtil"));

var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext"));

// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
var count = 1; // We still use class here since `forwardRef` not support generic in typescript

var Select = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Select, _React$Component);

  var _super = (0, _createSuper2["default"])(Select);

  function Select() {
    var _this;

    (0, _classCallCheck2["default"])(this, Select);
    _this = _super.apply(this, arguments);
    _this.selectRef = /*#__PURE__*/React.createRef();
    _this.state = {
      multiValues: _this.props.defaultValue || [],
      searchValue: '',
      options: []
    };

    _this.focus = function () {
      if (_this.selectRef.current) {
        _this.selectRef.current.focus();
      }
    };

    _this.blur = function () {
      if (_this.selectRef.current) {
        _this.selectRef.current.blur();
      }
    };

    _this.getMode = function () {
      var mode = _this.props.mode;

      if (mode === 'combobox') {
        return undefined;
      }

      if (mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE) {
        return 'combobox';
      }

      return mode;
    };

    _this.handleChange = function (values, option) {
      var newValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
      var onChange = _this.props.onChange;

      if (count) {
        // 避免输入不存在的值时触发两次 onChange 事件
        count = newValue ? 1 : -1;
      }

      if (count === 1) {
        _this.setState({
          multiValues: values,
          options: option
        });
      }

      if (onChange && count) {
        onChange(values, (0, _lodash.isArray)(values) ? values.map(function (item) {
          return (0, _lodash.find)(option, {
            value: item
          }) || {
            value: item
          };
        }) : option);
      }

      count += 1;
    };

    _this.onSearch = function (searchValue) {
      _this.setState({
        searchValue: searchValue
      });

      var _this$state = _this.state,
          multiValues = _this$state.multiValues,
          options = _this$state.options;
      var _this$props = _this.props,
          _this$props$tokenSepa = _this$props.tokenSeparators,
          tokenSeparators = _this$props$tokenSepa === void 0 ? [] : _this$props$tokenSepa,
          onSearch = _this$props.onSearch;

      var mode = _this.getMode();

      if (onSearch) onSearch(searchValue);

      if (!(0, _lodash.isEmpty)(tokenSeparators) && mode === 'multiple') {
        var separators = (0, _lodash.intersection)((0, _lodash.split)(searchValue, ''), tokenSeparators);
        var values = (0, _lodash.split)(searchValue, separators[0]).filter(function (item) {
          return item;
        });

        if (!(0, _lodash.isEmpty)(separators)) {
          _this.handleChange((0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(multiValues), (0, _toConsumableArray2["default"])(values)))), values.map(function (item) {
            return (0, _lodash.find)(options, {
              value: item
            }) || {
              value: item
            };
          }));

          _this.setState({
            searchValue: ''
          }, function () {
            _this.blur();

            _this.focus();
          });
        }
      }
    };

    _this.onInputKeyDown = function (e) {
      e.persist();
      var _this$state2 = _this.state,
          multiValues = _this$state2.multiValues,
          searchValue = _this$state2.searchValue,
          options = _this$state2.options;
      var _this$props2 = _this.props,
          onInputKeyDown = _this$props2.onInputKeyDown,
          onChange = _this$props2.onChange;
      if (onInputKeyDown) onInputKeyDown(e);

      if (e.keyCode === 13 && searchValue) {
        var values = (0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(multiValues), [searchValue])));

        if (onChange) {
          _this.handleChange(values, values.map(function (item) {
            return (0, _lodash.find)(options, {
              value: item
            }) || {
              value: item
            };
          }), false);
        }

        _this.setState({
          multiValues: values,
          searchValue: ''
        }, function () {
          _this.blur();

          _this.focus();
        });
      }
    };

    _this.renderSelect = function (_ref) {
      var getContextPopupContainer = _ref.getPopupContainer,
          getPrefixCls = _ref.getPrefixCls,
          direction = _ref.direction;
      var _this$props3 = _this.props,
          customizePrefixCls = _this$props3.prefixCls,
          notFoundContent = _this$props3.notFoundContent,
          className = _this$props3.className,
          customizeSize = _this$props3.size,
          _this$props3$listHeig = _this$props3.listHeight,
          listHeight = _this$props3$listHeig === void 0 ? 256 : _this$props3$listHeig,
          _this$props3$listItem = _this$props3.listItemHeight,
          listItemHeight = _this$props3$listItem === void 0 ? 32 : _this$props3$listItem,
          getPopupContainer = _this$props3.getPopupContainer,
          dropdownClassName = _this$props3.dropdownClassName,
          bordered = _this$props3.bordered,
          addNewValue = _this$props3.addNewValue;
      var prefixCls = getPrefixCls('select', customizePrefixCls);

      var mode = _this.getMode();

      var isMultiple = mode === 'multiple' || mode === 'tags'; // ===================== Empty =====================

      var mergedNotFound;

      if (notFoundContent !== undefined) {
        mergedNotFound = notFoundContent;
      } else if (mode === 'combobox') {
        mergedNotFound = null;
      } else {
        // mergedNotFound = renderEmpty('Select');
        mergedNotFound = '暂无数据';
      } // ===================== Icons =====================


      var _getIcons = (0, _iconUtil["default"])((0, _extends2["default"])((0, _extends2["default"])({}, _this.props), {
        multiple: isMultiple,
        prefixCls: prefixCls,
        bordered: bordered
      })),
          itemIcon = _getIcons.itemIcon,
          suffixIcon = _getIcons.suffixIcon,
          removeIcon = _getIcons.removeIcon,
          clearIcon = _getIcons.clearIcon; // ===================== addNewValue =====================


      var selectProps = (0, _omit["default"])(_this.props, ['prefixCls', 'suffixIcon', 'itemIcon', 'removeIcon', 'clearIcon', 'size', 'bordered', 'addNewValue']);

      if (mode === 'multiple' && addNewValue) {
        selectProps = (0, _extends2["default"])((0, _extends2["default"])({}, (0, _omit["default"])(selectProps, ['tokenSeparators', 'value', 'onSearch', 'onChange', 'onInputKeyDown'])), {
          value: _this.state.multiValues,
          onSearch: _this.onSearch,
          onChange: _this.handleChange,
          onInputKeyDown: _this.onInputKeyDown
        });
      }

      var rcSelectRtlDropDownClassName = (0, _classnames["default"])(dropdownClassName, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-dropdown-").concat(direction), direction === 'rtl'));
      return /*#__PURE__*/React.createElement(_SizeContext["default"].Consumer, null, function (size) {
        var _classNames2;

        var mergedSize = customizeSize || size;
        var mergedClassName = (0, _classnames["default"])(className, (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-lg"), mergedSize === 'large'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-sm"), mergedSize === 'small'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-md"), mergedSize === 'middle'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-borderless"), !bordered), _classNames2));
        return /*#__PURE__*/React.createElement(_rcSelect["default"], (0, _extends2["default"])({
          ref: _this.selectRef
        }, selectProps, {
          listHeight: listHeight,
          listItemHeight: listItemHeight,
          mode: mode,
          prefixCls: prefixCls,
          direction: direction,
          inputIcon: suffixIcon,
          menuItemSelectedIcon: itemIcon,
          removeIcon: removeIcon,
          clearIcon: clearIcon,
          notFoundContent: mergedNotFound,
          className: mergedClassName,
          getPopupContainer: getPopupContainer || getContextPopupContainer,
          dropdownClassName: rcSelectRtlDropDownClassName
        }));
      });
    };

    return _this;
  }

  (0, _createClass2["default"])(Select, [{
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, this.renderSelect);
    }
  }]);
  return Select;
}(React.Component);

Select.Option = _rcSelect.Option;
Select.OptGroup = _rcSelect.OptGroup;
Select.SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
Select.defaultProps = {
  transitionName: 'slide-up',
  choiceTransitionName: 'zoom',
  bordered: true,
  addNewValue: false
};
var _default = Select;
exports["default"] = _default;