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); }
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; }
/* eslint-disable no-case-declarations */
import { Badge, Button, Checkbox, Col, Divider, Dropdown, Popover } from 'antd';
import React, { useEffect, useId, useMemo, useState } from 'react';
import Icon from "../../Icon";
import { useFilterStyles } from "../styles";
var RenderContent = function RenderContent(props) {
  var values = props.values,
    setValues = props.setValues,
    current = props.current,
    fieldKey = props.fieldKey;
  var _useFilterStyles = useFilterStyles(),
    styles = _useFilterStyles.styles;
  var id = useId();
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    checkboxValues = _useState2[0],
    setCheckBoxValues = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    open = _useState4[0],
    setOpen = _useState4[1];
  useEffect(function () {
    if (current.componentType === 'Checkbox' && values[fieldKey]) {
      setCheckBoxValues(values[fieldKey]);
    }
    return function () {
      setCheckBoxValues([]);
    };
  }, [values]);
  var handleChangeValue = function handleChangeValue(e) {
    switch (current.componentType) {
      case 'Checkbox':
      case 'StatusSelect':
      case 'Select':
        setValues(function (values) {
          return JSON.parse(JSON.stringify(_objectSpread(_objectSpread({}, values), {}, _defineProperty({}, fieldKey, e.length ? e : undefined))));
        });
        setOpen(false);
        break;
      default:
        break;
    }
  };
  var getRenderDom = useMemo(function () {
    var _componentProps$field, _componentProps$field2, _componentProps$optio, _componentProps$field3, _componentProps$field4, _componentProps$optio2, _componentProps$field5, _componentProps$field6, _componentProps$optio3;
    var componentType = current.componentType,
      componentProps = current.componentProps;
    switch (componentType) {
      case 'StatusSelect':
        var labelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field = componentProps.fieldNames) === null || _componentProps$field === void 0 ? void 0 : _componentProps$field.label) || 'label';
        var valueKey = (componentProps === null || componentProps === void 0 || (_componentProps$field2 = componentProps.fieldNames) === null || _componentProps$field2 === void 0 ? void 0 : _componentProps$field2.value) || 'value';
        return /*#__PURE__*/React.createElement(Dropdown, {
          open: open,
          onOpenChange: function onOpenChange(openValue) {
            setOpen(openValue);
          },
          overlayStyle: componentProps === null || componentProps === void 0 ? void 0 : componentProps.dropdownStyle,
          trigger: ['click'],
          menu: {
            activeKey: values[fieldKey],
            style: _objectSpread({
              minWidth: '120px',
              maxWidth: '300px'
            }, componentProps === null || componentProps === void 0 ? void 0 : componentProps.dropdownStyle),
            onClick: function onClick(value) {
              handleChangeValue(value.key);
            },
            items: componentProps === null || componentProps === void 0 || (_componentProps$optio = componentProps.options) === null || _componentProps$optio === void 0 ? void 0 : _componentProps$optio.map(function (i) {
              return {
                label: /*#__PURE__*/React.createElement(Badge, {
                  color: i.color,
                  text: i[labelKey]
                }),
                key: i[valueKey]
              };
            })
          }
        }, /*#__PURE__*/React.createElement(Button, {
          icon: values[fieldKey] ? /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter",
            className: "hc-icon-active"
          }) : /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter"
          }),
          type: "text"
        }));
      case 'Select':
        var SelectlabelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field3 = componentProps.fieldNames) === null || _componentProps$field3 === void 0 ? void 0 : _componentProps$field3.label) || 'label';
        var SelectvalueKey = (componentProps === null || componentProps === void 0 || (_componentProps$field4 = componentProps.fieldNames) === null || _componentProps$field4 === void 0 ? void 0 : _componentProps$field4.value) || 'value';
        return /*#__PURE__*/React.createElement(Dropdown, {
          open: open,
          onOpenChange: function onOpenChange(openValue) {
            setOpen(openValue);
          },
          trigger: ['click'],
          menu: {
            activeKey: values[fieldKey],
            style: _objectSpread({
              minWidth: '120px',
              maxWidth: '20px'
            }, componentProps === null || componentProps === void 0 ? void 0 : componentProps.dropdownStyle),
            onClick: function onClick(value) {
              handleChangeValue(value.key);
            },
            items: componentProps === null || componentProps === void 0 || (_componentProps$optio2 = componentProps.options) === null || _componentProps$optio2 === void 0 ? void 0 : _componentProps$optio2.map(function (i) {
              return {
                label: i[SelectlabelKey],
                key: i[SelectvalueKey]
              };
            })
          }
        }, /*#__PURE__*/React.createElement(Button, {
          icon: values[fieldKey] ? /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter",
            className: "hc-icon-active"
          }) : /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter"
          }),
          type: "text"
        }));
      case 'Checkbox':
        var CheckboxlabelKey = (componentProps === null || componentProps === void 0 || (_componentProps$field5 = componentProps.fieldNames) === null || _componentProps$field5 === void 0 ? void 0 : _componentProps$field5.label) || 'label';
        var CheckboxvalueKey = (componentProps === null || componentProps === void 0 || (_componentProps$field6 = componentProps.fieldNames) === null || _componentProps$field6 === void 0 ? void 0 : _componentProps$field6.value) || 'value';
        return /*#__PURE__*/React.createElement(Popover, {
          destroyTooltipOnHide: true,
          open: open,
          onOpenChange: function onOpenChange(openValue) {
            setOpen(openValue);
          },
          overlayInnerStyle: componentProps === null || componentProps === void 0 ? void 0 : componentProps.dropdownStyle,
          trigger: "click",
          placement: "bottomLeft",
          arrow: false,
          key: id,
          content: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Checkbox.Group, {
            value: checkboxValues,
            onChange: function onChange(value) {
              setCheckBoxValues(value);
            },
            style: {
              display: 'block'
            }
          }, componentProps === null || componentProps === void 0 || (_componentProps$optio3 = componentProps.options) === null || _componentProps$optio3 === void 0 ? void 0 : _componentProps$optio3.map(function (item) {
            return /*#__PURE__*/React.createElement(Col, {
              key: item[CheckboxvalueKey]
            }, /*#__PURE__*/React.createElement(Checkbox, {
              value: item[CheckboxvalueKey]
            }, item[CheckboxlabelKey]));
          })), /*#__PURE__*/React.createElement(Divider, {
            style: {
              margin: '8px 0'
            }
          }), /*#__PURE__*/React.createElement("div", {
            style: {
              display: 'flex',
              justifyContent: 'end',
              alignItems: 'center',
              gap: '12px'
            }
          }, /*#__PURE__*/React.createElement(Button, {
            type: "primary",
            onClick: function onClick() {
              handleChangeValue(checkboxValues);
            },
            size: "small"
          }, "\u786E\u8BA4"), /*#__PURE__*/React.createElement(Button, {
            onClick: function onClick() {
              setCheckBoxValues([]);
              handleChangeValue([]);
            },
            size: "small"
          }, "\u53D6\u6D88"))),
          className: styles.container
        }, /*#__PURE__*/React.createElement(Button, {
          icon: values[fieldKey] ? /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter",
            className: "hc-icon-active"
          }) : /*#__PURE__*/React.createElement(Icon, {
            name: "v35_Filter"
          }),
          type: "text"
        }));
      default:
        return /*#__PURE__*/React.createElement("div", null);
    }
  }, [current, checkboxValues, open]);
  return getRenderDom;
};
export default RenderContent;