"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HtmlTable = HtmlTable;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
var _flat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/flat"));
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireDefault(require("react"));
var _internals = require("../internals");
var _colgroup = require("./colgroup");
var _SpanManager = _interopRequireDefault(require("./helpers/SpanManager"));
var _styles = require("./styles");
function HtmlTable(_ref) {
  var tbodyHtmlTag = _ref.tbodyHtmlTag,
    getRowProps = _ref.getRowProps,
    primaryKey = _ref.primaryKey,
    stickyRightOffset = _ref.stickyRightOffset,
    data = _ref.data,
    verInfo = _ref.verticalRenderInfo,
    hozInfo = _ref.horizontalRenderInfo,
    tbodyPosition = _ref.tbodyPosition;
  var flat = (0, _flat.default)(hozInfo),
    hoz = hozInfo.horizontalRenderRange;
  var spanManager = new _SpanManager.default();
  var fullFlatCount = flat.full.length;
  var leftFlatCount = flat.left.length;
  var rightFlatCount = flat.right.length;
  return /*#__PURE__*/_react.default.createElement("table", null, /*#__PURE__*/_react.default.createElement(_colgroup.Colgroup, {
    descriptors: hozInfo.visible
  }), /*#__PURE__*/_react.default.createElement(tbodyHtmlTag, null, (0, _map.default)(data).call(data, renderRow)));
  function renderRow(record, i) {
    var _context;
    var rowIndex = verInfo.offset + i;
    spanManager.stripUpwards(rowIndex);
    var rowProps = getRowProps(record, rowIndex);
    var rowClass = (0, _classnames.default)(_styles.Classes.tableRow, (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)({}, _styles.Classes.first, rowIndex === verInfo.first), _styles.Classes.last, rowIndex === verInfo.last), _styles.Classes.even, rowIndex % 2 === 0), _styles.Classes.odd, rowIndex % 2 === 1), rowProps === null || rowProps === void 0 ? void 0 : rowProps.className);
    var visibleColumnDescriptor = (0, _concat.default)(_context = hozInfo.visible).call(_context);
    // 左中右区域渲染，存在融合单元格时需要适配rowspan属性
    // 如果固定的列均存在融合单元格，需空白一列做占位,否则融合的单元格不会渲染，导致显示异常
    // 这里无法区分是否存在融合列，默认左右固定区域均添加占位空白列
    if (['left', 'right'].indexOf(tbodyPosition) > -1) {
      visibleColumnDescriptor.push({
        type: 'blank',
        blankSide: 'left',
        width: 0,
        isPlacehoder: true
      });
    }
    return /*#__PURE__*/_react.default.createElement("tr", (0, _extends2.default)({}, rowProps, {
      className: rowClass,
      key: (rowProps === null || rowProps === void 0 ? void 0 : rowProps['data-row-detail-key']) ? rowProps['data-row-detail-key'] : _internals.internals.safeGetRowKey(primaryKey, record, rowIndex),
      "data-rowindex": rowIndex,
      "data-role": 'table-row'
    }), (0, _map.default)(visibleColumnDescriptor).call(visibleColumnDescriptor, function (descriptor) {
      if (descriptor.type === 'blank') {
        return /*#__PURE__*/_react.default.createElement("td", {
          key: descriptor.blankSide,
          style: {
            visibility: descriptor.isPlacehoder ? 'hidden' : undefined
          }
        });
      }
      return renderBodyCell(record, rowIndex, descriptor.col, descriptor.colIndex);
    }));
  }
  function renderBodyCell(record, rowIndex, column, colIndex) {
    var _a, _b, _c;
    if (spanManager.testSkip(rowIndex, colIndex)) {
      return null;
    }
    var value = _internals.internals.safeGetValue(column, record, rowIndex);
    var cellProps = (_b = (_a = column.getCellProps) === null || _a === void 0 ? void 0 : _a.call(column, value, record, rowIndex)) !== null && _b !== void 0 ? _b : {};
    var cellContent = value;
    if (column.render) {
      cellContent = column.render(value, record, rowIndex);
    }
    var colSpan = 1;
    var rowSpan = 1;
    if (column.getSpanRect) {
      var spanRect = column.getSpanRect(value, record, rowIndex);
      colSpan = spanRect == null ? 1 : spanRect.right - colIndex;
      rowSpan = spanRect == null ? 1 : spanRect.bottom - rowIndex;
    } else {
      if (cellProps.colSpan != null) {
        colSpan = cellProps.colSpan;
      }
      if (cellProps.rowSpan != null) {
        rowSpan = cellProps.rowSpan;
      }
    }
    var hasSpan = colSpan > 1 || rowSpan > 1;
    if (hasSpan) {
      spanManager.add(rowIndex, colIndex, colSpan, rowSpan);
    }
    // rowSpan/colSpan 不能过大，避免 rowSpan/colSpan 影响因虚拟滚动而未渲染的单元格
    rowSpan = Math.min(rowSpan, verInfo.limit - rowIndex);
    colSpan = Math.min(colSpan, hozInfo.visible.length - colIndex);
    // todo: 右侧有列固定的情况下colSpan计算不对，这里先限制一下
    rowSpan = Math.max(rowSpan, 1);
    colSpan = Math.max(colSpan, 1);
    var positionStyle = {};
    if (colIndex < leftFlatCount) {
      positionStyle.position = 'sticky';
      positionStyle.left = hozInfo.stickyLeftMap.get(colIndex);
    } else if (colIndex >= fullFlatCount - rightFlatCount) {
      positionStyle.position = 'sticky';
      positionStyle.right = hozInfo.stickyRightMap.get(colIndex) + (typeof stickyRightOffset === 'number' ? stickyRightOffset : 0);
    }
    return /*#__PURE__*/_react.default.createElement('td', (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({
      key: colIndex
    }, cellProps), {
      className: (0, _classnames.default)(_styles.Classes.tableCell, cellProps.className, (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)({}, _styles.Classes.first, colIndex === 0), _styles.Classes.last, colIndex + colSpan === fullFlatCount), _styles.Classes.lockLeft, colIndex < leftFlatCount || tbodyPosition === 'left'), _styles.Classes.lockRight, colIndex >= fullFlatCount - rightFlatCount), _styles.Classes.rowSpan, rowSpan > 1))
    }), hasSpan ? {
      colSpan: colSpan,
      rowSpan: rowSpan
    } : null), {
      style: (0, _extends2.default)((0, _extends2.default)({
        textAlign: column.align,
        verticalAlign: (_c = column.verticalAlign) !== null && _c !== void 0 ? _c : 'middle'
      }, cellProps.style), positionStyle),
      'data-role': 'table-cell',
      'data-rowindex': rowIndex,
      'data-code': column.code
    }), tbodyPosition === 'center' && positionStyle.position === 'sticky' ? null : cellContent);
  }
}