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 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 _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); }
/* eslint-disable no-case-declarations */

import { Divider, Space, Tag } from 'antd';
import React, { useCallback, useMemo } from 'react';
import Icon from "../../Icon";
import { useTagsStyles } from "../style";
var CombineTags = function CombineTags(_ref) {
  var combineValues = _ref.combineValues,
    setCombineValues = _ref.setCombineValues,
    options = _ref.options,
    fieldNames = _ref.fieldNames,
    setCurrent = _ref.setCurrent;
  var _useTagsStyles = useTagsStyles(),
    styles = _useTagsStyles.styles;
  var getFindOption = useCallback(function (value, componentProps) {
    var _componentProps$optio;
    var findOption = componentProps === null || componentProps === void 0 || (_componentProps$optio = componentProps.options) === null || _componentProps$optio === void 0 ? void 0 : _componentProps$optio.find(function (option) {
      var _componentProps$field;
      if (componentProps.fieldNames && componentProps !== null && componentProps !== void 0 && (_componentProps$field = componentProps.fieldNames) !== null && _componentProps$field !== void 0 && _componentProps$field.label) {
        return option[componentProps.fieldNames.value].toString() === value.toString();
      }
      return option.value.toString() === value.toString();
    });
    return findOption;
  }, []);
  var getValues = useCallback(function (value, key, option) {
    var _option$componentProp, _getFindOption, _componentProps$field2;
    switch (option.componentType) {
      case 'StatusSelect':
      case 'Select':
        var labelKey = ((_option$componentProp = option.componentProps) === null || _option$componentProp === void 0 || (_option$componentProp = _option$componentProp.fieldNames) === null || _option$componentProp === void 0 ? void 0 : _option$componentProp.label) || 'label';
        return (_getFindOption = getFindOption(value, option.componentProps)) === null || _getFindOption === void 0 ? void 0 : _getFindOption[labelKey];
      case 'Checkbox':
        var componentProps = option.componentProps;
        var CheckboxLabelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field2 = componentProps.fieldNames) === null || _componentProps$field2 === void 0 ? void 0 : _componentProps$field2.label) || 'label';
        var values = value.map(function (i) {
          var _getFindOption2;
          return (_getFindOption2 = getFindOption(i, componentProps)) === null || _getFindOption2 === void 0 ? void 0 : _getFindOption2[CheckboxLabelKey];
        }).join(' ｜ ');
        return values;
      case 'DatePick':
        return value;
      case 'RangePick':
        return value.join(' - ');
      default:
        return value;
    }
  }, []);
  var TagsMemo = useMemo(function () {
    var keys = Object.keys(combineValues).map(function (key) {
      var findOption = options.find(function (i) {
        return i[fieldNames.value] === key;
      });
      if (findOption) {
        return /*#__PURE__*/React.createElement(Tag, {
          onClick: function onClick() {
            setCurrent(key);
          },
          className: styles.currentTag,
          key: key,
          closeIcon: /*#__PURE__*/React.createElement("span", {
            className: styles.closeIcon
          }, /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Failure_Filled"
          })),
          onClose: function onClose() {
            setCombineValues(function (values) {
              return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, key, undefined))));
            });
            if (findOption[fieldNames.value] === key) {
              setCurrent('');
            }
          },
          closable: true
        }, /*#__PURE__*/React.createElement(Space, {
          size: 0
        }, /*#__PURE__*/React.createElement("span", {
          className: styles.label
        }, findOption[fieldNames.label], "\uFF1A"), /*#__PURE__*/React.createElement("span", {
          className: styles.value
        }, getValues(combineValues[key], key, findOption))));
      }
      return null;
    });
    return keys;
  }, [JSON.stringify(options), JSON.stringify(combineValues), fieldNames]);
  return /*#__PURE__*/React.createElement("div", {
    style: {
      marginTop: '8px',
      display: 'flex',
      flexWrap: 'wrap',
      marginBottom: '8px'
    }
  }, TagsMemo, Object.keys(combineValues).length ? /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center',
      width: '60px'
    },
    onClick: function onClick() {
      setCombineValues({});
      setCurrent('');
    }
  }, /*#__PURE__*/React.createElement(Divider, {
    type: "vertical"
  }), /*#__PURE__*/React.createElement(Icon, {
    name: "v35_TrashCan",
    style: {
      cursor: 'pointer'
    }
  }), /*#__PURE__*/React.createElement("p", {
    style: {
      fontSize: '12px',
      marginLeft: '4px',
      cursor: 'pointer'
    }
  }, "\u6E05\u7A7A")) : null);
};
export default CombineTags;