"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

require("./index.scss");

var _components = require("@alicloudfe/components");

var _field = require("../../../field");

var _InfoValueItem = _interopRequireDefault(require("../InfoValueItem"));

var _2 = require("../../..");

var _utils = require("../../utils");

var _lodash = _interopRequireDefault(require("lodash.clonedeep"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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; }

var ProTableInfo = function ProTableInfo(props) {
  var columns = props.columns,
      dataSource = props.dataSource,
      _props$layout = props.layout,
      layout = _props$layout === void 0 ? {} : _props$layout,
      loading = props.loading,
      result = props.result,
      actionRef = props.actionRef,
      context = props.context;
  var _layout$colNum = layout.colNum,
      colNum = _layout$colNum === void 0 ? 2 : _layout$colNum,
      _layout$labelGutter = layout.labelGutter,
      labelGutter = _layout$labelGutter === void 0 ? '80px' : _layout$labelGutter;

  var getNewColumns = function getNewColumns() {
    // 需要 fill 填充 否则有可能不执行
    return _toConsumableArray(new Array(colNum)).fill(1).map(function (_, index) {
      return [{
        dataIndex: "label_".concat(index),
        align: 'left',
        height: 50,
        width: labelGutter
      }, {
        dataIndex: "value_".concat(index),
        align: 'left'
      }];
    }).reduce(function (acc, cur) {
      return [].concat(_toConsumableArray(acc), _toConsumableArray(cur));
    }, []);
  };

  var getInfoValueItem = function getInfoValueItem(item) {
    var _item$valueType;

    return /*#__PURE__*/_react.default.createElement(_InfoValueItem.default, {
      type: (_item$valueType = item === null || item === void 0 ? void 0 : item.valueType) !== null && _item$valueType !== void 0 ? _item$valueType : 'text',
      value: (0, _utils.getDataIndexValue)(item.dataIndex, dataSource !== null && dataSource !== void 0 ? dataSource : result),
      render: item.render,
      dataSource: item.dataSource,
      infoItem: item,
      record: dataSource !== null && dataSource !== void 0 ? dataSource : result,
      actionRef: actionRef,
      props: item.props
    });
  };

  var newDataSource = (0, _react.useMemo)(function () {
    var nResult = [];
    var columnsCopy = (0, _lodash.default)(columns).filter(function (item) {
      return !(0, _utils.isHidden)(item.hidden, (0, _utils.getDataIndexValue)(item.dataIndex, dataSource !== null && dataSource !== void 0 ? dataSource : result), dataSource !== null && dataSource !== void 0 ? dataSource : result, context);
    }); // 取当前行的数据。直到取完

    while (columnsCopy.length > 0) {
      var filterColumns = [];
      var col = colNum * 2; // 需要判断是否有合并行情况

      while (col >= 2) {
        if (columnsCopy.length > 0) {
          var _columnsItem$colSpan;

          var columnsItem = columnsCopy.shift();
          var needCol = 1 + ((_columnsItem$colSpan = columnsItem === null || columnsItem === void 0 ? void 0 : columnsItem.colSpan) !== null && _columnsItem$colSpan !== void 0 ? _columnsItem$colSpan : 1); // 如果剩余的col能满足下一组数据那么就放在这一组

          if (needCol <= col) {
            col = col - needCol;
            filterColumns.push(columnsItem);
          } else {
            var _filterColumns$0$colS;

            // 不满足条件。再安回去。。。
            columnsCopy.unshift(columnsItem); // 并且把剩余的col都分配给前一组数据

            filterColumns[0].colSpan = ((_filterColumns$0$colS = filterColumns[0].colSpan) !== null && _filterColumns$0$colS !== void 0 ? _filterColumns$0$colS : 1) + col; // 置0

            col = 0;
          }
        }

        if (columnsCopy.length === 0) {
          // 如果剩余仍有列数并且已经没有columns分配了
          col = 0;
        }
      }

      var itemData = filterColumns.map(function (item, index) {
        var _item$title, _data;

        var data = (_data = {}, _defineProperty(_data, "label_".concat(index), /*#__PURE__*/_react.default.createElement(_field.LabelIconTip, {
          label: (_item$title = item === null || item === void 0 ? void 0 : item.title) !== null && _item$title !== void 0 ? _item$title : '',
          tooltip: item.tooltip,
          icon: item.tooltipIcon
        })), _defineProperty(_data, "value_".concat(index), /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loading && /*#__PURE__*/_react.default.createElement(_2.ProSkeletonRaw.Info.Item, null), !loading && getInfoValueItem(item))), _data);

        if (item.colSpan) {
          data['formatter'] = {
            colSpan: item.colSpan
          };
        }

        return data;
      }).reduce(function (acc, cur) {
        return _objectSpread(_objectSpread({}, acc), cur);
      }, {});
      nResult.push(itemData);
    }

    return nResult;
  }, [loading, result]);

  var setCellProps = function setCellProps(rowIndex, colIndex, dataIndex, record) {
    if (colIndex % 2 === 0) {
      return {
        className: 'key-type'
      };
    }

    if (record.formatter) {
      if (record.formatter.colSpan) {
        if (colIndex === record.formatter.colSpan) {
          return {
            colSpan: record.formatter.colSpan
          };
        }

        var length = Object.keys(record).length;

        if (length <= 3) {
          return {
            colSpan: record.formatter.colSpan
          };
        }

        if (length > 5) {
          return {
            colSpan: record.formatter.colSpan
          };
        }
      }

      return {
        rowSpan: record.formatter.rowSpan
      };
    }
  };

  return /*#__PURE__*/_react.default.createElement(_components.Table.StickyLock, {
    className: "teamix-pro-info-table",
    cellProps: setCellProps,
    columns: getNewColumns(),
    hasHeader: false,
    hasBorder: true,
    dataSource: newDataSource
  });
};

var _default = ProTableInfo;
exports.default = _default;