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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Badge, Button, Checkbox, DatePicker, Divider, Input, Select, Space } from 'antd';
import React, { useEffect, useMemo, useState } from 'react';
var CombineContent = function CombineContent(props) {
  var current = props.current,
    onChangeState = props.onChangeState,
    setCombineValues = props.setCombineValues,
    combineValues = props.combineValues,
    fieldNames = props.fieldNames;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    checkboxValues = _useState2[0],
    setCheckBoxValues = _useState2[1];
  if (!current) {
    return null;
  }
  var componentProps = current.componentProps,
    componentType = current.componentType;
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  var handleChangeValue = function handleChangeValue(e) {
    switch (componentType) {
      case 'Checkbox':
      case 'StatusSelect':
      case 'Select':
        onChangeState(function () {
          setCombineValues(function (values) {
            return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, current[fieldNames.value], e.length ? e : undefined))));
          });
        });
        break;
      case 'RangePick':
        onChangeState(function () {
          setCombineValues(function (values) {
            return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, current[fieldNames.value], e.length ? e : undefined))));
          });
        });
        break;
      case 'DatePick':
        onChangeState(function () {
          setCombineValues(function (values) {
            return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, current[fieldNames.value], e ? e : undefined))));
          });
        });
        break;
      default:
        onChangeState(function () {
          setCombineValues(function (values) {
            var _e$target;
            return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, current[fieldNames.value], ((_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.value) || undefined))));
          });
        });
        break;
    }
  };
  useEffect(function () {
    if (componentType === 'Checkbox' && combineValues[current[fieldNames.value]]) {
      setCheckBoxValues(combineValues[current[fieldNames.value]]);
    }
    return function () {
      setCheckBoxValues([]);
    };
  }, []);
  var MemoComponent = useMemo(function () {
    switch (componentType) {
      case 'DatePick':
        return /*#__PURE__*/React.createElement(DatePicker, _extends({}, componentProps, {
          onChange: function onChange(date, dateString) {
            handleChangeValue(dateString ? dateString : '');
            if (componentProps !== null && componentProps !== void 0 && componentProps.onChange) {
              componentProps === null || componentProps === void 0 || componentProps.onChange(date, dateString);
            }
          },
          variant: "borderless",
          defaultOpen: true
        }));
      case 'RangePick':
        return /*#__PURE__*/React.createElement(DatePicker.RangePicker, _extends({}, componentProps, {
          onChange: function onChange(dates, dateStrings) {
            handleChangeValue(dates ? dateStrings : []);
            if (componentProps !== null && componentProps !== void 0 && componentProps.onChange) {
              componentProps === null || componentProps === void 0 || componentProps.onChange(dates, dateStrings);
            }
          },
          variant: "borderless",
          defaultOpen: true
        }));
      case 'Checkbox':
        {
          var _componentProps$field, _componentProps$field2;
          var CheckboxlabelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field = componentProps.fieldNames) === null || _componentProps$field === void 0 ? void 0 : _componentProps$field.label) || 'label';
          var CheckboxvalueKey = (componentProps === null || componentProps === void 0 || (_componentProps$field2 = componentProps.fieldNames) === null || _componentProps$field2 === void 0 ? void 0 : _componentProps$field2.value) || 'value';
          return /*#__PURE__*/React.createElement(Select, _extends({
            mode: "multiple",
            style: {
              width: '100%',
              padding: 0
            }
          }, componentProps, {
            value: checkboxValues,
            onChange: function onChange(values) {
              setCheckBoxValues(values);
            },
            dropdownRender: function dropdownRender(menu) {
              return /*#__PURE__*/React.createElement(React.Fragment, null, menu, /*#__PURE__*/React.createElement(Divider, {
                style: {
                  margin: '8px 0'
                }
              }), /*#__PURE__*/React.createElement("div", {
                style: {
                  display: 'flex',
                  justifyContent: 'end',
                  alignItems: 'center',
                  gap: '6px'
                }
              }, /*#__PURE__*/React.createElement(Button, {
                style: {
                  borderRadius: '4px'
                },
                type: "primary",
                onClick: function onClick() {
                  handleChangeValue(checkboxValues);
                },
                size: "small"
              }, "\u786E\u8BA4"), /*#__PURE__*/React.createElement(Button, {
                style: {
                  borderRadius: '4px'
                },
                onClick: function onClick() {
                  setCheckBoxValues([]);
                },
                size: "small"
              }, "\u91CD\u7F6E")));
            },
            defaultOpen: true,
            menuItemSelectedIcon: null,
            optionRender: function optionRender(option) {
              return /*#__PURE__*/React.createElement(Space, {
                align: "center"
              }, /*#__PURE__*/React.createElement(Checkbox, {
                key: JSON.stringify(option),
                checked: checkboxValues.includes(option.data[CheckboxvalueKey]),
                value: option.data[CheckboxvalueKey]
              }), option.data[CheckboxlabelKey]);
            },
            popupMatchSelectWidth: 320,
            suffixIcon: false,
            variant: "borderless"
          }));
        }
      case 'StatusSelect':
        {
          var _componentProps$field3, _componentProps$field4, _componentProps$optio;
          var labelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field3 = componentProps.fieldNames) === null || _componentProps$field3 === void 0 ? void 0 : _componentProps$field3.label) || 'label';
          var valueKey = (componentProps === null || componentProps === void 0 || (_componentProps$field4 = componentProps.fieldNames) === null || _componentProps$field4 === void 0 ? void 0 : _componentProps$field4.value) || 'value';
          return /*#__PURE__*/React.createElement(Select, _extends({
            style: {
              width: '100%',
              padding: 0
            }
          }, componentProps, {
            defaultOpen: true,
            onSelect: function onSelect() {
              for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
                arg[_key] = arguments[_key];
              }
              handleChangeValue.apply(void 0, arg);
              if (componentProps !== null && componentProps !== void 0 && componentProps.onSelect) {
                componentProps === null || componentProps === void 0 || componentProps.onSelect.apply(componentProps, arg);
              }
            },
            options: componentProps === null || componentProps === void 0 || (_componentProps$optio = componentProps.options) === null || _componentProps$optio === void 0 ? void 0 : _componentProps$optio.map(function (item) {
              return _defineProperty(_defineProperty({}, labelKey, /*#__PURE__*/React.createElement(Badge, {
                color: item.color,
                text: item[labelKey]
              })), valueKey, item[valueKey]);
            }),
            defaultValue: combineValues[current[fieldNames.value]],
            popupMatchSelectWidth: 320,
            suffixIcon: false,
            variant: "borderless"
          }));
        }
      case 'Select':
        return /*#__PURE__*/React.createElement(Select, _extends({
          style: {
            width: '100%',
            padding: 0
          }
        }, componentProps, {
          defaultOpen: true,
          onSelect: function onSelect() {
            for (var _len2 = arguments.length, arg = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
              arg[_key2] = arguments[_key2];
            }
            handleChangeValue.apply(void 0, arg);
            if (componentProps !== null && componentProps !== void 0 && componentProps.onSelect) {
              componentProps === null || componentProps === void 0 || componentProps.onSelect.apply(componentProps, arg);
            }
          },
          defaultValue: combineValues[current[fieldNames.value]],
          popupMatchSelectWidth: 320,
          suffixIcon: false,
          variant: "borderless"
        }));
      default:
        return /*#__PURE__*/React.createElement(Input, _extends({}, componentProps, {
          variant: "borderless",
          allowClear: false,
          defaultValue: combineValues[current[fieldNames.value]],
          onKeyDown: function onKeyDown() {
            for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
              args[_key3] = arguments[_key3];
            }
            //@ts-ignore
            if (args[0].code === 'Backspace' && !args[0].target.value) {
              //@ts-ignore
              handleChangeValue('');
            }
            if (componentProps !== null && componentProps !== void 0 && componentProps.onKeyDown) {
              //@ts-ignore
              componentProps === null || componentProps === void 0 || componentProps.onKeyDown.apply(componentProps, args);
            }
          },
          onPressEnter: function onPressEnter(e) {
            if (e.nativeEvent.isComposing) {
              return;
            }
            // @ts-ignore
            handleChangeValue(e);
          },
          autoFocus: true
        }));
    }
  }, [current, combineValues, fieldNames, checkboxValues]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, MemoComponent);
};
export default CombineContent;