"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _select = _interopRequireDefault(require("antd/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var React = _interopRequireWildcard(require("react"));
require("./index.less");
var _search = _interopRequireDefault(require("./componments/search"));
var _twoSelect = _interopRequireDefault(require("./componments/twoSelect"));
var _excluded = ["ref", "style", "notFoundContent", "labelInValue", "options", "status", "defaultValue", "hideSelected", "loading", "showArrow", "tokenSeparators", "optionLabelProp", "optionFilterProp", "firstOptions", "dropdownRender", "EndOptions", "autoFocus", "placement", "maxTagCount", "filterSort", "maxTagPlaceholder", "tagRender", "maxTagTextLength", "defaultOpen", "allowClear", "size", "bordered", "open", "value", "showSearch", "placeholder", "disabled", "autoClearSearchValue", "mode"];
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
var antSelect = function antSelect(_ref) {
  var ref = _ref.ref,
    style = _ref.style,
    notFoundContent = _ref.notFoundContent,
    labelInValue = _ref.labelInValue,
    options = _ref.options,
    status = _ref.status,
    defaultValue = _ref.defaultValue,
    hideSelected = _ref.hideSelected,
    loading = _ref.loading,
    showArrow = _ref.showArrow,
    tokenSeparators = _ref.tokenSeparators,
    optionLabelProp = _ref.optionLabelProp,
    optionFilterProp = _ref.optionFilterProp,
    firstOptions = _ref.firstOptions,
    dropdownRender = _ref.dropdownRender,
    EndOptions = _ref.EndOptions,
    autoFocus = _ref.autoFocus,
    placement = _ref.placement,
    maxTagCount = _ref.maxTagCount,
    filterSort = _ref.filterSort,
    maxTagPlaceholder = _ref.maxTagPlaceholder,
    tagRender = _ref.tagRender,
    maxTagTextLength = _ref.maxTagTextLength,
    defaultOpen = _ref.defaultOpen,
    allowClear = _ref.allowClear,
    size = _ref.size,
    bordered = _ref.bordered,
    open = _ref.open,
    value = _ref.value,
    showSearch = _ref.showSearch,
    placeholder = _ref.placeholder,
    disabled = _ref.disabled,
    autoClearSearchValue = _ref.autoClearSearchValue,
    mode = _ref.mode,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _useState = (0, React.useState)(value || []),
    SelectValue = _useState[0],
    setSelectValue = _useState[1];
  var _useState2 = (0, React.useState)(false),
    showSearch01 = _useState2[0],
    setshowSearch01 = _useState2[1];
  var _useState3 = (0, React.useState)([]),
    filterOptions = _useState3[0],
    setFilterOptions = _useState3[1];
  var _style = style || {
    minWidth: '200px'
  };
  var _otherProps = otherProps || {};
  _otherProps.style = _style;
  var handleChange = function handleChange(value) {
    console.log("selected " + value);
    setSelectValue(value);
  };
  (0, React.useEffect)(function () {
    if (mode == 'Search') {
      setshowSearch01(true);
    }
    if (hideSelected && ['multiple'].includes(mode)) {
      var filteredOptions = options.filter(function (o) {
        return !SelectValue.includes(o.value || o.label);
      });
      setFilterOptions(filteredOptions);
    }
  }, [hideSelected, options, SelectValue, mode]);

  //修复删除默认字段会转成对象的问题
  function ChangeArray(data) {
    var newValue = [];
    if (Array.isArray(data)) {
      data.map(function (item) {
        if (typeof item === 'object') {
          var str = Object.values(item).join('');
          newValue.push(str);
        } else {
          newValue.push(item);
        }
      });
      return newValue;
    } else {
      return data;
    }
  }
  //------------------------------带搜索框的下拉框------------------------------------------
  var handleSearch = function handleSearch(value) {
    console.log('search:', value);
  };
  var RenderSelect = function RenderSelect() {
    //基本下拉框属性
    var defaultProps = (0, _extends2["default"])({
      style: _otherProps.style,
      disabled: disabled,
      status: status,
      autoFocus: autoFocus,
      bordered: bordered,
      placement: placement,
      dropdownMatchSelectWidth: false,
      // open,
      notFoundContent: notFoundContent,
      size: size,
      //defaultActiveFirstOption,
      allowClear: allowClear,
      loading: loading,
      placeholder: placeholder,
      labelInValue: labelInValue,
      dropdownRender: dropdownRender
    }, otherProps);
    (defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.length) > 0 || allowClear ? defaultProps.defaultValue = ChangeArray(defaultValue) : defaultProps.value = ChangeArray(SelectValue);

    //下拉箭头showArrow 和loading 也互斥
    showArrow ? defaultProps.showArrow = showArrow : defaultProps.loading = loading;
    //open为false 会影响 allowClear
    // open ? defaultProps.open =open:defaultProps.defaultOpen =defaultOpen
    if (['Search'].includes(mode)) {
      var searchProps = (0, _extends2["default"])({
        showSearch: showSearch01,
        options: options,
        loading: loading,
        optionLabelProp: optionLabelProp,
        optionFilterProp: optionFilterProp,
        onChange: handleChange,
        onSearch: handleSearch,
        placeholder: placeholder
      }, defaultProps);
      console.log(searchProps, '----------...searchProps');
      return /*#__PURE__*/React.createElement(_search["default"], (0, _extends2["default"])({}, searchProps, {
        filterSort: function filterSort(optionA, optionB) {
          var _optionA$label, _optionB$label;
          return ((_optionA$label = optionA === null || optionA === void 0 ? void 0 : optionA.label) !== null && _optionA$label !== void 0 ? _optionA$label : '').toLowerCase().localeCompare(((_optionB$label = optionB === null || optionB === void 0 ? void 0 : optionB.label) !== null && _optionB$label !== void 0 ? _optionB$label : '').toLowerCase());
        }
      }));
    } else if (['multiple', 'tags'].includes(mode)) {
      //多选下拉框
      var modeProps = (0, _extends2["default"])({
        mode: mode,
        maxTagCount: maxTagCount,
        maxTagPlaceholder: maxTagPlaceholder,
        maxTagTextLength: maxTagTextLength,
        autoClearSearchValue: autoClearSearchValue,
        tokenSeparators: tokenSeparators,
        tagRender: tagRender,
        options: hideSelected ? filterOptions : options
      }, defaultProps);
      console.log(modeProps, '----------...modeProps');
      return /*#__PURE__*/React.createElement(_select["default"], modeProps);
    } else if (['TwoSelect'].includes(mode)) {
      //联动下拉框
      var twoSelectProps = (0, _extends2["default"])({
        options: options
      }, defaultProps);
      console.log(twoSelectProps, '----------...twoSelectProps');
      return /*#__PURE__*/React.createElement(_twoSelect["default"], twoSelectProps);
    } else {
      defaultProps.options = options;
      console.log(defaultProps, '----------...defaultProps');
      return /*#__PURE__*/React.createElement(_select["default"], defaultProps);
    }
  };
  return RenderSelect();
};
antSelect.displayName = 'antSelect';
var _default = antSelect;
exports["default"] = _default;