"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _data = require("../../data");
var _FieldComponent = _interopRequireDefault(require("../FieldComponent"));
var _style = _interopRequireDefault(require("./style"));
var _classnames = _interopRequireDefault(require("classnames"));
var _StandardTitle = _interopRequireDefault(require("../../StandardTitle"));
var _icons = require("@ant-design/icons");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 _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; }
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 _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); } /* eslint-disable */ // import CustomFormList from './CustomFormList';
var FormItem = _antd.Form.Item;

// 单个控件配置参数

// interface DescType {
//   label?: any
//   description?: any
// }
//
// const DescriptionTooltip = (props: DescType) => {
//   const { label, description } = props;
//   return <span className={'spec-item-group-label'} style={{ display: 'inline-flex', alignItems: 'center' }}>{label} {description && <Tooltip title={description}><SpecIcon name="info" style={{ marginLeft: label ? '4px' : 0 }}/></Tooltip>}</span>
// }

var RenterToolTip = function RenterToolTip(param) {
  if (!param) return null;
  if (_typeof(param) === 'object' && !param.hasOwnProperty('icon')) {
    return _objectSpread({
      icon: /*#__PURE__*/_react.default.createElement(_icons.InfoCircleOutlined, null)
    }, param);
  }
  return param;
};
var RenderItemGroup = function RenderItemGroup(props) {
  var itemGroup = props.itemGroup,
    _props$size = props.size,
    size = _props$size === void 0 ? 'md' : _props$size,
    _props$fieldStatus = props.fieldStatus,
    fieldStatus = _props$fieldStatus === void 0 ? 'default' : _props$fieldStatus,
    _props$fieldSize = props.fieldSize,
    fieldSize = _props$fieldSize === void 0 ? 'middle' : _props$fieldSize,
    displayLayout = props.displayLayout,
    form = props.form,
    doUpdateFormFields = props.doUpdateFormFields,
    _props$layout = props.layout,
    layout = _props$layout === void 0 ? "horizontal" : _props$layout,
    gutter = props.gutter,
    columnCount = props.columnCount,
    parentNode = props.parentNode,
    onFieldsChange = props.onFieldsChange;
  var componentName = 'spec-item-group';
  var _useStyle = (0, _style.default)(componentName),
    _useStyle2 = _slicedToArray(_useStyle, 2),
    wrapSSR = _useStyle2[0],
    hashId = _useStyle2[1];
  var setColSpan = function setColSpan(display, modalSize, columnCount) {
    if (_typeof(display) === 'object' || display === 'block') {
      // 栅格数量为：1
      return 24;
    }
    if (columnCount) {
      return 24 / columnCount; // 2, 3, 4, 6, 8, 12
    }
    if (modalSize === 'xs' || modalSize === 'sm' || modalSize === 'md' || modalSize === 'lg' || modalSize === 'xlg' || modalSize === 'xxlg') {
      return 24 / _data.general.modalSize[modalSize].columnNumber;
    }
    // if (modalSize === 'xs' || modalSize === 'sm') {
    //   // 栅格数量为：1
    //   return 24
    // }
    // if (modalSize === 'lg' || modalSize === 'xlg' || modalSize === 'xxlg') {
    //   // 栅格数量为：3
    //   return 8
    // }
    // 栅格数量为：2
    return 12;
  };
  var updateFormFields = function updateFormFields() {
    // if (newVal) form && form.setFieldsValue({ ...newVal })
    if (doUpdateFormFields) doUpdateFormFields(); // 用于触发更新表单实例赋值
  };
  (0, _react.useEffect)(function () {
    updateFormFields();
  }, [form]);

  // 渲染隐藏指定控件
  var itemIsHide = function itemIsHide(isHide, getFieldsValue, FormInstance) {
    if (typeof isHide === 'function') {
      return isHide(getFieldsValue, FormInstance, fieldStatus);
    }
    return isHide || false;
  };

  // 计算表单栅格数排版
  // 优先级 displayLayout.side > displayLayout > general
  // 优先级：若有displayLayout.block 则直接使用 general > displayLayout
  // 还需要判断 layout为vertical还是horizontal
  var calculateFormLayout = (0, _react.useCallback)(function (itemData) {
    var _general$modalSize$md, _general$modalSize$md2, _general$modalSize$lg, _general$modalSize$lg2;
    var formLayout = {};
    // 当布局为 'inline' 时，不配置栅格结构
    if (layout === 'inline') {
      formLayout = {};
    } else if (layout === 'vertical') {
      var _itemData$protoFormIt;
      if (itemData !== null && itemData !== void 0 && (_itemData$protoFormIt = itemData.protoFormItem) !== null && _itemData$protoFormIt !== void 0 && _itemData$protoFormIt.layout) {
        formLayout = {};
      } else {
        formLayout = {
          labelCol: {
            span: 24
          },
          wrapperCol: {
            span: 24
          }
        };
      }
    } else {
      var _general$modalSize, _general$modalSize2;
      // @ts-ignore
      var sizeFormLayout = (_general$modalSize = _data.general.modalSize) === null || _general$modalSize === void 0 || (_general$modalSize = _general$modalSize[size]) === null || _general$modalSize === void 0 ? void 0 : _general$modalSize[parentNode === 'standardFilter' && (size === 'md' || size === 'lg' || size === 'xlg') ? 'filterFormLayout' : 'formLayout'];
      // @ts-ignore
      var sizeFormBlockLayout = (_general$modalSize2 = _data.general.modalSize) === null || _general$modalSize2 === void 0 || (_general$modalSize2 = _general$modalSize2[size]) === null || _general$modalSize2 === void 0 ? void 0 : _general$modalSize2[parentNode === 'standardFilter' && (size === 'md' || size === 'lg' || size === 'xlg') ? 'filterFormBlockLayout' : 'formBlockLayout'];
      // 优先级 displayLayout.side > displayLayout > general
      // 优先级：若有displayLayout.block 则直接使用 general > displayLayout
      // 还需要判断 layout为vertical还是horizontal
      if (columnCount) {
        // 有配置栅格数 1,2,3,4
        switch (columnCount) {
          case 1:
            sizeFormLayout = _data.general.modalSize.sm.formLayout;
            sizeFormBlockLayout = _data.general.modalSize.sm.formBlockLayout;
            break;
          case 2:
            sizeFormLayout = (_general$modalSize$md = _data.general.modalSize.md) === null || _general$modalSize$md === void 0 ? void 0 : _general$modalSize$md[parentNode === 'standardFilter' ? 'filterFormLayout' : 'formLayout'];
            sizeFormBlockLayout = (_general$modalSize$md2 = _data.general.modalSize.md) === null || _general$modalSize$md2 === void 0 ? void 0 : _general$modalSize$md2[parentNode === 'standardFilter' ? 'filterFormBlockLayout' : 'formBlockLayout'];
            break;
          case 3:
            sizeFormLayout = (_general$modalSize$lg = _data.general.modalSize.lg) === null || _general$modalSize$lg === void 0 ? void 0 : _general$modalSize$lg[parentNode === 'standardFilter' ? 'filterFormLayout' : 'formLayout'];
            sizeFormBlockLayout = (_general$modalSize$lg2 = _data.general.modalSize.lg) === null || _general$modalSize$lg2 === void 0 ? void 0 : _general$modalSize$lg2[parentNode === 'standardFilter' ? 'filterFormBlockLayout' : 'formBlockLayout'];
            break;
          case 4:
            sizeFormLayout = _data.general.modalSize.xxlg.formLayout;
            sizeFormBlockLayout = _data.general.modalSize.xxlg.formBlockLayout;
            break;
        }
      }
      formLayout = (displayLayout === null || displayLayout === void 0 ? void 0 : displayLayout.side) || (displayLayout !== null && displayLayout !== void 0 && displayLayout.block ? sizeFormLayout : !displayLayout || JSON.stringify(displayLayout) === '{}' ? sizeFormLayout : displayLayout);
      if (itemData.hasOwnProperty('display')) {
        if ((itemData === null || itemData === void 0 ? void 0 : itemData.display) === 'block') {
          // @ts-ignore
          formLayout = (displayLayout === null || displayLayout === void 0 ? void 0 : displayLayout.block) || sizeFormBlockLayout;
        } else if ((itemData === null || itemData === void 0 ? void 0 : itemData.display) === 'side') {
          // 使用默认
          // formLayout = displayLayout?.side || (displayLayout?.block ? general.modalSize[size].formLayout : displayLayout) || general.modalSize[size].formLayout
          // formLayout = displayLayout?.side || (displayLayout?.block ? general.modalSize[size].formLayout : ((!displayLayout || JSON.stringify(displayLayout) === '{}') ? general.modalSize[size].formLayout : displayLayout))
        } else {
          // 对象格式
          formLayout = itemData === null || itemData === void 0 ? void 0 : itemData.display; // 对象类型传值如： {labelCol: {span: 3},wrapperCol: {span: 8}} 优先级最高
        }
      }
    }
    return formLayout;
  }, [size, layout, columnCount, parentNode]);

  // 渲染表单控件
  var renderFormField = function renderFormField(field) {
    var _general$inlineFormIt;
    var componentStyle = field.componentStyle,
      extra = field.extra,
      preExtra = field.preExtra; // 当前可传参的配置值
    // 表单控件样式默认最小宽，如有单独配置width时，则不配置
    // @ts-ignore
    var FieldDefaultWidth = layout === 'inline' ? _objectSpread({}, (_general$inlineFormIt = _data.general.inlineFormItemStyle) === null || _general$inlineFormIt === void 0 ? void 0 : _general$inlineFormIt[size]) : _objectSpread({}, _data.general.inlineFormItemStyle.wide); // componentStyle?.width ? {} : { width: layout === 'inline' ? general.formItemStyle?.[size].width : general.formItemStyle.wide.width }
    // 带前缀或后缀控件渲染
    if (preExtra || extra) {
      return /*#__PURE__*/_react.default.createElement(_antd.Form.Item, {
        noStyle: true
      }, preExtra ? preExtra(field, form) : '', /*#__PURE__*/_react.default.createElement(FormItem, {
        noStyle: true,
        rules: field.rules || (field.fieldOptions ? field.fieldOptions.rules : []),
        name: field.filedName || field.fieldName
      }, /*#__PURE__*/_react.default.createElement(_FieldComponent.default, _extends({}, field, {
        // @ts-ignore
        onChange: function onChange(eOrValue) {
          updateFormFields();
          for (var _len = arguments.length, otherParams = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
            otherParams[_key - 1] = arguments[_key];
          }
          if (field.onChange) field.onChange.apply(field, [eOrValue].concat(otherParams));
          if (field.filedName || field.fieldName) {
            // @ts-ignore
            onFieldsChange === null || onFieldsChange === void 0 || onFieldsChange(_defineProperty({}, field.filedName || field.fieldName, eOrValue));
          }
        },
        fieldStatus: field.fieldStatus || fieldStatus,
        size: field.protoConfig && field.protoConfig.size || field.size || fieldSize
        // @ts-ignore
        ,
        style: componentStyle || {}
        //      style={componentStyle || { ...general.formItemStyle.wide }}
        //      style={componentStyle || ( layout === 'inline' ? { ...general.inlineFormItemStyle?.[size] } : {...general.inlineFormItemStyle.wide})}
        ,
        defaultStyle: componentStyle ? {} : FieldDefaultWidth
      }))), extra ? extra(field, form) : '');
    } else {
      return /*#__PURE__*/_react.default.createElement(_FieldComponent.default, _extends({}, field, {
        // @ts-ignore
        onChange: function onChange(eOrValue) {
          // let updateValue = val
          // if (field.type === 'input' || field.type === 'textarea' || field.type === 'radio' || field.type === 'password') {
          //   updateValue = val.target.value
          // }
          // updateFormFields({ [field.filedName || field.fieldName]: updateValue })
          updateFormFields();
          // if (field.onChange) field.onChange(val, form)
          for (var _len2 = arguments.length, otherParams = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
            otherParams[_key2 - 1] = arguments[_key2];
          }
          if (field.onChange) field.onChange.apply(field, [eOrValue].concat(otherParams));
          // @ts-ignore
          if (field.filedName || field.fieldName) onFieldsChange === null || onFieldsChange === void 0 || onFieldsChange(_defineProperty({}, field.filedName || field.fieldName, eOrValue));
        },
        fieldStatus: field.fieldStatus || fieldStatus,
        size: field.protoConfig && field.protoConfig.size || field.size || fieldSize,
        style: componentStyle || {}
        // style={componentStyle || { ...general.formItemStyle.wide }}
        // style={componentStyle || ( layout === 'inline' ? { ...general.inlineFormItemStyle?.[size] } : {...general.inlineFormItemStyle.wide})}
        ,
        defaultStyle: componentStyle ? {} : FieldDefaultWidth
      }));
    }
  };
  var renderColFormItem = function renderColFormItem(item, index, formLayout) {
    var label = item.label,
      description = item.description,
      type = item.type,
      customRender = item.customRender,
      filedName = item.filedName,
      fieldName = item.fieldName; // 当前可传参的配置值
    if (layout === "horizontal" || layout === "vertical") {
      return /*#__PURE__*/_react.default.createElement(_antd.Col, _extends({
        key: index,
        span: item.colSpan || setColSpan(item.display, size, columnCount),
        style: item.style
      }, item.protoCol), /*#__PURE__*/_react.default.createElement(FormItem, _extends({
        name: type !== 'custom' && (filedName || fieldName),
        className: (0, _classnames.default)(fieldStatus === 'viewOnly' ? 'form-item-view-only' : '', 'spec-item-group-form-item')
        // label={description ? <DescriptionTooltip label={label} description={description}/> : label}
        ,
        label: label,
        tooltip: RenterToolTip(description),
        rules: item.preExtra || item.extra ? [] : item.rules || (item.fieldOptions ? item.fieldOptions.rules : []),
        required: item.rules && item.rules.filter(function (rItem) {
          return rItem.required;
        }).length > 0
      }, formLayout, item.protoFormItem),
      // type === 'custom' && customRender ? customRender(form, { ...item }, fieldStatus) : type === 'FormList' ? <CustomFormList name={filedName || fieldName} {...item}/> : renderFormField({ ...item })
      type === 'custom' && customRender ? customRender(form, _objectSpread({}, item), fieldStatus, {
        onFieldsChange: onFieldsChange
      }) : renderFormField(_objectSpread({}, item))));
    } else {
      return /*#__PURE__*/_react.default.createElement(FormItem, _extends({
        key: index,
        name: type !== 'custom' && (filedName || fieldName),
        className: (0, _classnames.default)(fieldStatus === 'viewOnly' ? 'form-item-view-only' : '', 'spec-item-group-form-item')
        // label={description ? <DescriptionTooltip label={label} description={description}/> : label}
        ,
        tooltip: RenterToolTip(description),
        rules: item.rules || (item.fieldOptions ? item.fieldOptions.rules : []),
        label: label
      }, formLayout, item.protoFormItem),
      // type === 'custom' && customRender ? customRender(form, { ...item }, fieldStatus) : type === 'FormList' ? <CustomFormList name={filedName || fieldName} {...item}/>  : renderFormField({ ...item })
      type === 'custom' && customRender ? customRender(form, _objectSpread({}, item), fieldStatus, {
        onFieldsChange: onFieldsChange
      }) : renderFormField(_objectSpread({}, item)));
    }
  };

  // 渲染表单控件列表
  var renderItemGroupNode = function renderItemGroupNode() {
    return itemGroup.map(function (item, index) {
      var label = item.label,
        isHide = item.isHide,
        type = item.type,
        _item$display = item.display,
        display = _item$display === void 0 ? 'side' : _item$display; // 当前可传参的配置值
      var formLayout = calculateFormLayout(item);
      // title标题或html渲染
      if (type === 'title' || type === 'html' || type === 'StandardTitle') {
        return /*#__PURE__*/_react.default.createElement(FormItem, {
          key: index,
          noStyle: true,
          shouldUpdate: true
        }, function (FormInstance) {
          var _item$protoConfig, _item$protoConfig2, _item$protoConfig3;
          // isHide 返回为true则隐藏当前控件
          return !itemIsHide(isHide, FormInstance.getFieldValue(), FormInstance) ? /*#__PURE__*/_react.default.createElement(_antd.Col, _extends({
            span: item.colSpan || 24,
            style: item.style
          }, item.protoCol), type === 'StandardTitle' || type === 'title' && label === 'StandardTitle' ? /*#__PURE__*/_react.default.createElement(_StandardTitle.default, _extends({
            title: (item === null || item === void 0 || (_item$protoConfig = item.protoConfig) === null || _item$protoConfig === void 0 ? void 0 : _item$protoConfig.title) || label
          }, item.protoConfig, {
            className: (0, _classnames.default)('modalPanelTitle', (item === null || item === void 0 || (_item$protoConfig2 = item.protoConfig) === null || _item$protoConfig2 === void 0 ? void 0 : _item$protoConfig2.className) || '')
          })) : type === 'title' ? /*#__PURE__*/_react.default.createElement("h3", _extends({}, item.protoConfig, {
            className: (0, _classnames.default)('modalPanelTitle', (item === null || item === void 0 || (_item$protoConfig3 = item.protoConfig) === null || _item$protoConfig3 === void 0 ? void 0 : _item$protoConfig3.className) || '')
          }), label) : label) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, null);
        });
      }
      // 判断是否强制重新渲染表单
      if (item.protoFormItem && item.protoFormItem.shouldUpdate) {
        return /*#__PURE__*/_react.default.createElement(FormItem, {
          key: index,
          noStyle: true,
          shouldUpdate: item.protoFormItem.shouldUpdate
        }, function (FormInstance) {
          // isHide 返回为true则隐藏当前控件
          return !itemIsHide(isHide, FormInstance.getFieldValue(), FormInstance) ? renderColFormItem(item, index, formLayout) : null;
        });
      }
      // 常规表单项结构
      return renderColFormItem(item, index, formLayout);
    });
  };
  var itemGroupCls = (0, _classnames.default)(componentName, componentName + '-' + layout, props === null || props === void 0 ? void 0 : props.className, hashId);
  return wrapSSR( /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null,
  // itemGroup = Array 则按以下结构渲染
  itemGroup && Array.isArray(itemGroup) ? layout === 'inline' ?
  /*#__PURE__*/
  // renderItemGroupNode() // 不带标签
  _react.default.createElement("div", {
    className: itemGroupCls
  }, renderItemGroupNode())
  // 布局配置为horizontal或vertical
  : /*#__PURE__*/_react.default.createElement(_antd.Row, {
    className: itemGroupCls,
    gutter: layout === 'vertical' ? gutter || [16, 16] : gutter
  }, renderItemGroupNode())
  /* itemGroup = ReactNode则直接返回 */ : /*#__PURE__*/_react.default.createElement("div", {
    className: itemGroupCls
  }, itemGroup)));
};
var _default = exports.default = RenderItemGroup;