import { Grid } from '@alicloudfe/components';
import React from 'react';
import ProInfoItem from '../ProInfoItem';
import './index.scss';
import { getLayout } from '../../utils/utils';
import defaultLayoutMap from '../../utils/layout';
import InfoValueItem from '../InfoValueItem';
import { getDataIndexValue, isHidden } from '../../utils';
var Row = Grid.Row,
  Col = Grid.Col;
var ProBaseInfo = function ProBaseInfo(props) {
  var _size$width;
  var columns = props.columns,
    dataSource = props.dataSource,
    loading = props.loading,
    result = props.result,
    actionRef = props.actionRef,
    layout = props.layout,
    size = props.size,
    context = props.context,
    _props$disabledAutoSi = props.disabledAutoSize,
    disabledAutoSize = _props$disabledAutoSi === void 0 ? false : _props$disabledAutoSi;
  var defaultLayout = defaultLayoutMap[getLayout((_size$width = size === null || size === void 0 ? void 0 : size.width) !== null && _size$width !== void 0 ? _size$width : 0)];
  // 获取列内的布局参数
  var getFormItemLayout = function getFormItemLayout(colspan) {
    var formItemLayout = JSON.parse(JSON.stringify(defaultLayout.formItemLayout));
    // 自定义布局
    if (layout) {
      var _layout$labelCol, _layout$wrapperCol;
      var labelCol = (_layout$labelCol = layout === null || layout === void 0 ? void 0 : layout.labelCol) !== null && _layout$labelCol !== void 0 ? _layout$labelCol : defaultLayout.formItemLayout.labelCol;
      var wrapperCol = (_layout$wrapperCol = layout === null || layout === void 0 ? void 0 : layout.wrapperCol) !== null && _layout$wrapperCol !== void 0 ? _layout$wrapperCol : defaultLayout.formItemLayout.wrapperCol;
      formItemLayout.labelCol = labelCol;
      formItemLayout.wrapperCol = wrapperCol;
    }
    var newLabelSpan = formItemLayout.labelCol.span;
    var newWrapperSpan = formItemLayout.wrapperCol.span;
    // 计算合并列情况。label与wrapper需要重新计算
    if (colspan) {
      var _layout$span, _layout$span2;
      var realColspan = ((_layout$span = layout === null || layout === void 0 ? void 0 : layout.span) !== null && _layout$span !== void 0 ? _layout$span : 24 / defaultLayout.span > colspan) ? colspan : (_layout$span2 = layout === null || layout === void 0 ? void 0 : layout.span) !== null && _layout$span2 !== void 0 ? _layout$span2 : 24 / defaultLayout.span;
      newLabelSpan = newLabelSpan / realColspan;
      newWrapperSpan = newWrapperSpan + (formItemLayout.labelCol.span - newLabelSpan);
    }
    var newFormItemLayout = {
      labelCol: {
        span: newLabelSpan.toFixed()
      },
      wrapperCol: {
        span: newWrapperSpan.toFixed()
      }
    };
    return Object.assign(formItemLayout, newFormItemLayout);
  };
  var renderContent = function renderContent() {
    return columns.map(function (item, index) {
      var _ref, _ref2, _layout$span3, _layout$span4, _item$title, _item$valueType, _ref3, _item$colSpan;
      var hidden = isHidden(item === null || item === void 0 ? void 0 : item.hidden, getDataIndexValue(item.dataIndex, (_ref = dataSource !== null && dataSource !== void 0 ? dataSource : result) !== null && _ref !== void 0 ? _ref : {}), (_ref2 = dataSource !== null && dataSource !== void 0 ? dataSource : result) !== null && _ref2 !== void 0 ? _ref2 : {}, context);
      if (hidden) {
        return;
      }
      var span = item.colSpan ? defaultLayout.span : defaultLayout.span;
      var layoutSpan = item.colSpan ? ((_layout$span3 = layout === null || layout === void 0 ? void 0 : layout.span) !== null && _layout$span3 !== void 0 ? _layout$span3 : span) * item.colSpan : (_layout$span4 = layout === null || layout === void 0 ? void 0 : layout.span) !== null && _layout$span4 !== void 0 ? _layout$span4 : span;
      return /*#__PURE__*/React.createElement(Col, {
        span: layoutSpan > 24 ? 24 : layoutSpan,
        key: index
      }, /*#__PURE__*/React.createElement(ProInfoItem, {
        label: (_item$title = item === null || item === void 0 ? void 0 : item.title) !== null && _item$title !== void 0 ? _item$title : '',
        loading: loading,
        value: /*#__PURE__*/React.createElement(InfoValueItem, {
          type: (_item$valueType = item === null || item === void 0 ? void 0 : item.valueType) !== null && _item$valueType !== void 0 ? _item$valueType : 'text',
          value: getDataIndexValue(item.dataIndex, (_ref3 = dataSource !== null && dataSource !== void 0 ? dataSource : result) !== null && _ref3 !== void 0 ? _ref3 : {}),
          render: item.render,
          dataSource: item.dataSource,
          infoItem: item,
          record: dataSource !== null && dataSource !== void 0 ? dataSource : result,
          actionRef: actionRef,
          context: context,
          props: item.props
        }),
        baseInfoLayout: getFormItemLayout((_item$colSpan = item === null || item === void 0 ? void 0 : item.colSpan) !== null && _item$colSpan !== void 0 ? _item$colSpan : 0),
        tooltip: item.tooltip,
        tooltipIcon: item.tooltipIcon,
        render: item.render,
        valueType: item.valueType,
        alignItems: item.alignItems
      }));
    });
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, ((size === null || size === void 0 ? void 0 : size.width) || disabledAutoSize) && /*#__PURE__*/React.createElement(Row, {
    wrap: true,
    className: "teamix-pro-info-content-row",
    gutter: 20
  }, renderContent()));
};
export default ProBaseInfo;