"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
exports.uuid = uuid;
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", "DropbackgroundColor", "DropColor", "selectedStyle", "activeStyle", "disabledStyle"],
  _excluded2 = ["color", "fontSize", "fontWeight", "height", "lineHeight", "opacity", "textAlign"];
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,
    DropbackgroundColor = _ref.DropbackgroundColor,
    DropColor = _ref.DropColor,
    selectedStyle = _ref.selectedStyle,
    activeStyle = _ref.activeStyle,
    disabledStyle = _ref.disabledStyle,
    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 _useState4 = (0, React.useState)(''),
    ID = _useState4[0],
    setID = _useState4[1];
  var _useState5 = (0, React.useState)(false),
    opens = _useState5[0],
    setOpen = _useState5[1];
  var _style = style || {
    minWidth: '200px'
  };
  var _otherProps = (0, _extends2["default"])({}, otherProps);
  _otherProps.style = _style;
  var handleChange = function handleChange(value) {
    // console.log(`selected ${value}`);
    setSelectValue(value);
  };
  (0, React.useEffect)(function () {
    setSelectValue(value);
  }, [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]);
  (0, React.useEffect)(function () {
    setID("antSelect_" + uuid());
  }, []);
  (0, React.useEffect)(function () {
    ChangeSize();
    ChangeTextStyle();
    ChangeColorStyle();
    ChangeSelectedStyle();
  }, [ID, _otherProps.style]);
  function UtilsChangeStyle(name, params) {
    var _document;
    var elems = (_document = document) === null || _document === void 0 ? void 0 : _document.querySelectorAll(name);
    var index = 0,
      length = elems.length;
    for (; index < length; index++) {
      elems[index].style.backgroundColor = params.backgroundColor || "";
      elems[index].style.color = params.color || "";
      elems[index].style.fontSize = params.fontSize + 'px' || "";
    }
  }
  (0, React.useEffect)(function () {
    UtilsChangeStyle('.ant-select-item', {
      color: DropColor
    });
    UtilsChangeStyle('.ant-select-item-option-selected', selectedStyle);
    UtilsChangeStyle('.ant-select-item-option-disabled', disabledStyle);
    if (!opens) {
      return;
    } else {
      ChangeSize();
    }
    //监听鼠标的移入移出

    function ChangeSize() {
      setTimeout(function () {
        //先清空
        UtilsChangeStyle('.ant-select-item-option', {
          backgroundColor: '',
          color: '',
          fontSize: ''
        });
        setTimeout(function () {
          //鼠标移入，加载选中样式和移入样式
          UtilsChangeStyle('.ant-select-item', {
            color: DropColor
          });
          UtilsChangeStyle('.ant-select-item-option-disabled', disabledStyle);
          UtilsChangeStyle('.ant-select-item-option-active', activeStyle);
          UtilsChangeStyle('.ant-select-item-option-selected', selectedStyle);
        }, 0);
      }, 0);
    }
    document.getElementsByClassName('ant-select-dropdown')[0].addEventListener('mouseover', function () {
      ChangeSize();
    });
    //监听onclick事件
    document.getElementsByClassName('ant-select-dropdown')[0].addEventListener('click', function () {
      ChangeTextStyle();
      // setTimeout(()=>{
      //   //先清空
      //   UtilsChangeStyle('.ant-select-item-option',{
      //     backgroundColor: '',
      //     color:'',
      //     fontSize: ''
      //   })
      //   setTimeout(() => {
      //     //鼠标移入，加载选中样式和移入样式
      //     UtilsChangeStyle('.ant-select-item',{color:DropColor})
      //     UtilsChangeStyle('.ant-select-item-option-disabled', disabledStyle);
      //     UtilsChangeStyle('.ant-select-item-option-active',activeStyle)
      //     UtilsChangeStyle('.ant-select-item-option-selected',selectedStyle)

      //   }, 0);
      // },0)
    });
  }, [opens, DropbackgroundColor, DropColor, selectedStyle, activeStyle, disabledStyle]);
  var ChangeSize = function ChangeSize() {
    setTimeout(function () {
      if (ID) {
        var _ref2;
        // //1.控制下拉框的高度，选中组件设置高度后，应该拉高下拉框本身的高度。
        var elems = (_ref2 = document.getElementById(ID) || document) === null || _ref2 === void 0 ? void 0 : _ref2.querySelectorAll('.ant-select-selector,.ant-select');
        console.log(document.getElementById(ID).offsetParent.getBoundingClientRect().height, '---------document.getElementById(ID).parentNode');
        var index = 0,
          length = elems.length;
        for (; index < length; index++) {
          elems[index].style.height = _otherProps.style.height || ''; //高度
          elems[index].style.width = '100%'; //宽度
        }

        setTimeout(function () {
          var _ref3;
          var elems01 = (_ref3 = document.getElementById(ID) || document) === null || _ref3 === void 0 ? void 0 : _ref3.querySelectorAll('.ant-select-single .ant-select-selector .ant-select-selection-item, .ant-select-single .ant-select-selector .ant-select-selection-placeholder');
          var index01 = 0,
            length01 = elems01.length;
          for (; index01 < length01; index01++) {
            elems01[index01].style.lineHeight = _otherProps.style.height || ''; //高度
          }
        }, 0);
      }
    }, 0);
  };
  var ChangeTextStyle = function ChangeTextStyle() {
    //选中下拉框后，支持在右侧属性面板”样式”选项卡中文本属性：如字号、颜色等等：
    setTimeout(function () {
      var _ref4;
      var _otherProps$style = _otherProps.style,
        color = _otherProps$style.color,
        fontSize = _otherProps$style.fontSize,
        fontWeight = _otherProps$style.fontWeight,
        height = _otherProps$style.height,
        lineHeight = _otherProps$style.lineHeight,
        opacity = _otherProps$style.opacity,
        textAlign = _otherProps$style.textAlign,
        parentStyles = (0, _objectWithoutPropertiesLoose2["default"])(_otherProps$style, _excluded2);
      var elems = (_ref4 = document.getElementById(ID) || document) === null || _ref4 === void 0 ? void 0 : _ref4.querySelectorAll('.ant-select-selection-item ,.ant-select-selection-placeholder');
      var index = 0,
        length = elems.length;
      for (; index < length; index++) {
        elems[index].style.fontSize = fontSize || "";
        elems[index].style.color = color || "";
        elems[index].style.fontWeight = fontWeight || "";
        elems[index].style.lineHeight = height || "";
        elems[index].style.opacity = opacity || "";
        elems[index].style.textAlign = textAlign || "";
      }
    }, 0);
  };
  var ChangeColorStyle = function ChangeColorStyle() {
    //选中下拉框后，支持在右侧属性面板”样式”选项卡中文本属性：如字号、颜色等等：

    setTimeout(function () {
      var _ref5;
      var _otherProps$style2 = _otherProps.style,
        backgroundColor = _otherProps$style2.backgroundColor,
        borderBottomColor = _otherProps$style2.borderBottomColor,
        borderRadius = _otherProps$style2.borderRadius,
        borderLeftColor = _otherProps$style2.borderLeftColor,
        borderRightColor = _otherProps$style2.borderRightColor,
        borderTopColor = _otherProps$style2.borderTopColor,
        borderBottomWidth = _otherProps$style2.borderBottomWidth,
        borderTopWidth = _otherProps$style2.borderTopWidth,
        borderLeftWidth = _otherProps$style2.borderLeftWidth,
        borderRightWidth = _otherProps$style2.borderRightWidth,
        borderBottomStyle = _otherProps$style2.borderBottomStyle,
        borderTopStyle = _otherProps$style2.borderTopStyle,
        borderLeftStyle = _otherProps$style2.borderLeftStyle,
        borderRightStyle = _otherProps$style2.borderRightStyle,
        borderColor = _otherProps$style2.borderColor,
        borderStyle = _otherProps$style2.borderStyle,
        borderWidth = _otherProps$style2.borderWidth;
      var elems = (_ref5 = document.getElementById(ID) || document) === null || _ref5 === void 0 ? void 0 : _ref5.querySelectorAll('.ant-select:not(.ant-select-customize-input) .ant-select-selector');
      var index = 0,
        length = elems.length;
      for (; index < length; index++) {
        elems[index].style.borderColor = borderColor || "";
        elems[index].style.borderStyle = borderStyle || "";
        elems[index].style.borderWidth = borderWidth || "";
        elems[index].style.backgroundColor = backgroundColor || "";
        elems[index].style.borderRadius = borderRadius || "";
        if (borderBottomColor && borderBottomWidth && borderBottomStyle) {
          elems[index].style.borderBottomColor = borderBottomColor || '';
          elems[index].style.borderBottomWidth = borderBottomWidth || '';
          elems[index].style.borderBottomStyle = borderBottomStyle || '';
        }
        if (borderLeftColor && borderLeftWidth && borderLeftStyle) {
          elems[index].style.borderLeftColor = borderLeftColor || '';
          elems[index].style.borderLeftWidth = borderLeftWidth || '';
          elems[index].style.borderLeftStyle = borderLeftStyle || '';
        }
        if (borderRightColor && borderRightWidth && borderRightStyle) {
          elems[index].style.borderRightColor = borderRightColor || '';
          elems[index].style.borderRightWidth = borderRightWidth || '';
          elems[index].style.borderRightStyle = borderRightStyle || '';
        }
        if (borderTopColor && borderTopWidth && borderTopStyle) {
          elems[index].style.borderTopColor = borderTopColor || '';
          elems[index].style.borderTopWidth = borderTopWidth || '';
          elems[index].style.borderTopStyle = borderTopStyle || '';
        }
      }
    }, 0);
  };
  var ChangeSelectedStyle = function ChangeSelectedStyle() {
    //选中下拉框后，支持在右侧属性面板”属性”选项卡设置下拉框的选中效果。（选中后的背景色及字体颜色
    function comment(name, params) {
      setTimeout(function () {
        //1.清空其他元素的背景style
        UtilsChangeStyle('.ant-select-item-option', {
          backgroundColor: '',
          color: '',
          fontSize: ''
        });
        setTimeout(function () {
          //2.添加样式
          UtilsChangeStyle(name, params);
        }, 0);
      }, 0);
    }

    //选中的样式
    comment('.ant-select-item-option-selected', selectedStyle);
    comment('.ant-select-item', {
      color: DropColor
    });
    comment('.ant-select-item-option-selected .ant-select-item-option-active:not(.ant-select-item-option-disabled)', activeStyle);
    comment('.ant-select-item-option-disabled', disabledStyle);
  };
  //修复删除默认字段会转成对象的问题
  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 onDropdownVisibleChange = function onDropdownVisibleChange(value) {
    setOpen(value);
  };
  var RenderSelect = function RenderSelect() {
    //基本下拉框属性
    var defaultProps = (0, _extends2["default"])({
      // style: _otherProps.style,ant-select-selection-item
      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,
      onDropdownVisibleChange: onDropdownVisibleChange
    }, otherProps);
    if (!value) {
      //value 没有内容就输出defaultValue
      defaultProps.defaultValue = [];
    } else {
      (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;
      return /*#__PURE__*/React.createElement(_select["default"], (0, _extends2["default"])({}, defaultProps, {
        dropdownStyle: {
          backgroundColor: DropbackgroundColor
        }
      }));
    }
  };
  function autoSize(value1, value2) {
    var Value = value1;
    if (value1 && value1.includes('px')) {
      Value = value2 && Number(value1.split('px')[0]) + Number((value2 || 0).split('px')[0]) + 'px' || value1;
    } else if (value1 && value1.includes('%')) {
      Value = value2 && Number(value1.split('%')[0]) + Number((value2 || 0).split('%')[0]) + '%' || value1;
    } else {
      Value = "calc(" + value1 + "+ " + (value2 || '0px') + ")";
    }
    console.log(Value, '--------Value');
    return Value;
  }
  var layoutStyle = {
    //布局样式
    width: autoSize(_otherProps.style.width, _otherProps.style.paddingLeft) || '200px',
    // width:_otherProps.style.width||'200px',
    display: _otherProps.style.display || '',
    alignItems: _otherProps.style.alignItems || '',
    flexDirection: _otherProps.style.flexDirection,
    flexWrap: _otherProps.style.flexWrap,
    justifyContent: _otherProps.style.justifyContent,
    marginBottom: _otherProps.style.marginBottom,
    marginRight: _otherProps.style.marginRight,
    marginTop: _otherProps.style.marginTop,
    marginLeft: _otherProps.style.marginLeft,
    paddingTop: _otherProps.style.paddingTop,
    paddingBottom: _otherProps.style.paddingBottom,
    paddingLeft: _otherProps.style.paddingLeft,
    paddingRight: _otherProps.style.paddingRight
  };
  return /*#__PURE__*/React.createElement("div", {
    id: ID,
    style: layoutStyle
  }, RenderSelect());
};
antSelect.displayName = 'antSelect';
var _default = antSelect;
exports["default"] = _default;
function uuid() {
  return (Math.random() * 1e6 >> 0).toString(36);
}