"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SelectProps = exports.SelectValue = exports.AbstractSelectProps = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _warning = _interopRequireDefault(require("../_util/warning"));

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

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _vcSelect = require("../vc-select");

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

var _propsUtil = require("../_util/props-util");

var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons-vue/CloseOutlined"));

var _CloseCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/CloseCircleFilled"));

var _CheckOutlined = _interopRequireDefault(require("@ant-design/icons-vue/CheckOutlined"));

var _DownOutlined = _interopRequireDefault(require("@ant-design/icons-vue/DownOutlined"));

var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons-vue/LoadingOutlined"));

var _vnode = require("../_util/vnode");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var AbstractSelectProps = function AbstractSelectProps() {
  return {
    prefixCls: _vueTypes.default.string,
    size: _vueTypes.default.oneOf(['small', 'large', 'default']),
    showAction: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.arrayOf(String)]),
    notFoundContent: _vueTypes.default.any,
    transitionName: _vueTypes.default.string,
    choiceTransitionName: _vueTypes.default.string,
    showSearch: _vueTypes.default.bool,
    allowClear: _vueTypes.default.bool,
    disabled: _vueTypes.default.bool,
    tabindex: _vueTypes.default.number,
    placeholder: _vueTypes.default.any,
    defaultActiveFirstOption: _vueTypes.default.bool,
    dropdownClassName: _vueTypes.default.string,
    dropdownStyle: _vueTypes.default.any,
    dropdownMenuStyle: _vueTypes.default.any,
    dropdownMatchSelectWidth: _vueTypes.default.bool,
    // onSearch: (value: string) => any,
    filterOption: _vueTypes.default.oneOfType([_vueTypes.default.bool, _vueTypes.default.func]),
    autofocus: _vueTypes.default.bool,
    backfill: _vueTypes.default.bool,
    showArrow: _vueTypes.default.bool,
    getPopupContainer: _vueTypes.default.func,
    open: _vueTypes.default.bool,
    defaultOpen: _vueTypes.default.bool,
    autoClearSearchValue: _vueTypes.default.bool,
    dropdownRender: _vueTypes.default.func,
    loading: _vueTypes.default.bool
  };
};

exports.AbstractSelectProps = AbstractSelectProps;

var Value = _vueTypes.default.shape({
  key: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number])
}).loose;

var SelectValue = _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number, _vueTypes.default.arrayOf(_vueTypes.default.oneOfType([Value, _vueTypes.default.string, _vueTypes.default.number])), Value]);

exports.SelectValue = SelectValue;

var SelectProps = _objectSpread(_objectSpread({}, AbstractSelectProps()), {}, {
  value: SelectValue,
  defaultValue: SelectValue,
  // mode: PropTypes.oneOf(['default', 'multiple', 'tags', 'combobox']),
  mode: _vueTypes.default.string,
  optionLabelProp: _vueTypes.default.string,
  firstActiveValue: _vueTypes.default.oneOfType([String, _vueTypes.default.arrayOf(String)]),
  maxTagCount: _vueTypes.default.number,
  maxTagPlaceholder: _vueTypes.default.any,
  maxTagTextLength: _vueTypes.default.number,
  dropdownMatchSelectWidth: _vueTypes.default.bool,
  optionFilterProp: _vueTypes.default.string,
  labelInValue: _vueTypes.default.boolean,
  getPopupContainer: _vueTypes.default.func,
  tokenSeparators: _vueTypes.default.arrayOf(_vueTypes.default.string),
  getInputElement: _vueTypes.default.func,
  options: _vueTypes.default.array,
  suffixIcon: _vueTypes.default.any,
  removeIcon: _vueTypes.default.any,
  clearIcon: _vueTypes.default.any,
  menuItemSelectedIcon: _vueTypes.default.any
});

exports.SelectProps = SelectProps;
var SelectPropTypes = {
  prefixCls: _vueTypes.default.string,
  size: _vueTypes.default.oneOf(['default', 'large', 'small']),
  // combobox: PropTypes.bool,
  notFoundContent: _vueTypes.default.any,
  showSearch: _vueTypes.default.bool,
  optionLabelProp: _vueTypes.default.string,
  transitionName: _vueTypes.default.string,
  choiceTransitionName: _vueTypes.default.string
};
var SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
var Select = {
  SECRET_COMBOBOX_MODE_DO_NOT_USE: SECRET_COMBOBOX_MODE_DO_NOT_USE,
  Option: _objectSpread(_objectSpread({}, _vcSelect.Option), {}, {
    name: 'ASelectOption'
  }),
  OptGroup: _objectSpread(_objectSpread({}, _vcSelect.OptGroup), {}, {
    name: 'ASelectOptGroup'
  }),
  name: 'ASelect',
  props: _objectSpread(_objectSpread({}, SelectProps), {}, {
    showSearch: _vueTypes.default.bool.def(false),
    transitionName: _vueTypes.default.string.def('slide-up'),
    choiceTransitionName: _vueTypes.default.string.def('zoom')
  }),
  propTypes: SelectPropTypes,
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  created: function created() {
    (0, _vue.provide)('savePopupRef', this.savePopupRef);
    (0, _warning.default)(this.$props.mode !== 'combobox', 'Select', 'The combobox mode of Select is deprecated,' + 'it will be removed in next major version,' + 'please use AutoComplete instead');
  },
  methods: {
    getNotFoundContent: function getNotFoundContent(renderEmpty) {
      var notFoundContent = (0, _propsUtil.getComponent)(this, 'notFoundContent');

      if (notFoundContent !== undefined) {
        return notFoundContent;
      }

      if (this.isCombobox()) {
        return null;
      }

      return renderEmpty('Select');
    },
    savePopupRef: function savePopupRef(ref) {
      this.popupRef = ref;
    },
    focus: function focus() {
      this.$refs.vcSelect.focus();
    },
    blur: function blur() {
      this.$refs.vcSelect.blur();
    },
    isCombobox: function isCombobox() {
      var mode = this.mode;
      return mode === 'combobox' || mode === SECRET_COMBOBOX_MODE_DO_NOT_USE;
    },
    renderSuffixIcon: function renderSuffixIcon(prefixCls) {
      var loading = this.$props.loading;
      var suffixIcon = (0, _propsUtil.getComponent)(this, 'suffixIcon');
      suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;

      if (suffixIcon) {
        return (0, _propsUtil.isValidElement)(suffixIcon) ? (0, _vnode.cloneElement)(suffixIcon, {
          class: "".concat(prefixCls, "-arrow-icon")
        }) : suffixIcon;
      }

      if (loading) {
        return _vue.createVNode(_LoadingOutlined.default, null, null);
      }

      return _vue.createVNode(_DownOutlined.default, {
        "class": "".concat(prefixCls, "-arrow-icon")
      }, null);
    }
  },
  render: function render() {
    var _cls;

    var _getOptionProps = (0, _propsUtil.getOptionProps)(this),
        customizePrefixCls = _getOptionProps.prefixCls,
        size = _getOptionProps.size,
        mode = _getOptionProps.mode,
        options = _getOptionProps.options,
        getPopupContainer = _getOptionProps.getPopupContainer,
        showArrow = _getOptionProps.showArrow,
        restProps = _objectWithoutProperties(_getOptionProps, ["prefixCls", "size", "mode", "options", "getPopupContainer", "showArrow"]);

    var className = this.$attrs.class;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var renderEmpty = this.configProvider.renderEmpty;
    var prefixCls = getPrefixCls('select', customizePrefixCls);
    var getContextPopupContainer = this.configProvider.getPopupContainer;
    var removeIcon = (0, _propsUtil.getComponent)(this, 'removeIcon');
    removeIcon = Array.isArray(removeIcon) ? removeIcon[0] : removeIcon;
    var clearIcon = (0, _propsUtil.getComponent)(this, 'clearIcon');
    clearIcon = Array.isArray(clearIcon) ? clearIcon[0] : clearIcon;
    var menuItemSelectedIcon = (0, _propsUtil.getComponent)(this, 'menuItemSelectedIcon');
    menuItemSelectedIcon = Array.isArray(menuItemSelectedIcon) ? menuItemSelectedIcon[0] : menuItemSelectedIcon;
    var rest = (0, _omit.default)(restProps, ['inputIcon', 'removeIcon', 'clearIcon', 'suffixIcon', 'menuItemSelectedIcon']);
    var cls = (_cls = {}, _defineProperty(_cls, className, className), _defineProperty(_cls, "".concat(prefixCls, "-lg"), size === 'large'), _defineProperty(_cls, "".concat(prefixCls, "-sm"), size === 'small'), _defineProperty(_cls, "".concat(prefixCls, "-show-arrow"), showArrow), _cls);
    var optionLabelProp = this.$props.optionLabelProp;

    if (this.isCombobox()) {
      // children 带 dom 结构时，无法填入输入框
      optionLabelProp = optionLabelProp || 'value';
    }

    var modeConfig = {
      multiple: mode === 'multiple',
      tags: mode === 'tags',
      combobox: this.isCombobox()
    };

    var finalRemoveIcon = removeIcon && ((0, _propsUtil.isValidElement)(removeIcon) ? (0, _vnode.cloneElement)(removeIcon, {
      class: "".concat(prefixCls, "-remove-icon")
    }) : removeIcon) || _vue.createVNode(_CloseOutlined.default, {
      "class": "".concat(prefixCls, "-remove-icon")
    }, null);

    var finalClearIcon = clearIcon && ((0, _propsUtil.isValidElement)(clearIcon) ? (0, _vnode.cloneElement)(clearIcon, {
      class: "".concat(prefixCls, "-clear-icon")
    }) : clearIcon) || _vue.createVNode(_CloseCircleFilled.default, {
      "class": "".concat(prefixCls, "-clear-icon")
    }, null);

    var finalMenuItemSelectedIcon = menuItemSelectedIcon && ((0, _propsUtil.isValidElement)(menuItemSelectedIcon) ? (0, _vnode.cloneElement)(menuItemSelectedIcon, {
      class: "".concat(prefixCls, "-selected-icon")
    }) : menuItemSelectedIcon) || _vue.createVNode(_CheckOutlined.default, {
      "class": "".concat(prefixCls, "-selected-icon")
    }, null);

    var selectProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
      inputIcon: this.renderSuffixIcon(prefixCls),
      removeIcon: finalRemoveIcon,
      clearIcon: finalClearIcon,
      menuItemSelectedIcon: finalMenuItemSelectedIcon,
      showArrow: showArrow
    }, rest), modeConfig), {}, {
      prefixCls: prefixCls,
      optionLabelProp: optionLabelProp || 'children',
      notFoundContent: this.getNotFoundContent(renderEmpty),
      maxTagPlaceholder: (0, _propsUtil.getComponent)(this, 'maxTagPlaceholder'),
      placeholder: (0, _propsUtil.getComponent)(this, 'placeholder'),
      children: options ? options.map(function (option) {
        var key = option.key,
            _option$label = option.label,
            label = _option$label === void 0 ? option.title : _option$label,
            cls = option.class,
            style = option.style,
            restOption = _objectWithoutProperties(option, ["key", "label", "class", "style"]);

        return _vue.createVNode(_vcSelect.Option, _vue.mergeProps(_objectSpread({
          class: cls,
          style: style
        }, restOption), {
          "key": key
        }), {
          default: function _default() {
            return [label];
          }
        });
      }) : (0, _propsUtil.getSlot)(this),
      dropdownRender: (0, _propsUtil.getComponent)(this, 'dropdownRender', {}, false),
      getPopupContainer: getPopupContainer || getContextPopupContainer
    }, this.$attrs), {}, {
      class: cls,
      ref: 'vcSelect'
    });

    return _vue.createVNode(_vcSelect.Select, _vue.mergeProps(selectProps, {
      "__propsSymbol__": []
    }), null);
  }
};
/* istanbul ignore next */

Select.install = function (app) {
  app.component(Select.name, Select);
  app.component(Select.Option.name, Select.Option);
  app.component(Select.OptGroup.name, Select.OptGroup);
};

var _default2 = Select;
exports.default = _default2;