import _extends from "@babel/runtime-corejs3/helpers/extends";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _flatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import cx from 'classnames';
import React from 'react';
import { internals } from '../internals';
import { Colgroup } from './colgroup';
import SpanManager from './helpers/SpanManager';
import { Classes } from './styles';
export 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 = _flatInstanceProperty(hozInfo),
    hoz = hozInfo.horizontalRenderRange,
    direction = hozInfo.direction;
  var spanManager = new SpanManager();
  var fullFlatCount = flat.full.length;
  var leftFlatCount = flat.left.length;
  var rightFlatCount = flat.right.length;
  return /*#__PURE__*/React.createElement("table", null, /*#__PURE__*/React.createElement(Colgroup, {
    descriptors: hozInfo.visible
  }), /*#__PURE__*/React.createElement(tbodyHtmlTag, null, _mapInstanceProperty(data).call(data, renderRow)));
  function renderRow(record, i) {
    var _context, _context2;
    var rowIndex = verInfo.offset + i;
    spanManager.stripUpwards(rowIndex);
    var rowProps = getRowProps(record, rowIndex);
    var rowClass = cx(Classes.tableRow, _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Classes.first, rowIndex === verInfo.first), Classes.last, rowIndex === verInfo.last), Classes.even, rowIndex % 2 === 0), Classes.odd, rowIndex % 2 === 1), rowProps === null || rowProps === void 0 ? void 0 : rowProps.className);
    var visibleColumnDescriptor = _concatInstanceProperty(_context = hozInfo.visible).call(_context);
    // 左中右区域渲染，存在融合单元格时需要适配rowspan属性
    // 如果固定的列均存在融合单元格，需空白一列做占位,否则融合的单元格不会渲染，导致显示异常
    // 这里无法区分是否存在融合列，默认左右固定区域均添加占位空白列
    if (['left', 'right'].indexOf(tbodyPosition) > -1) {
      visibleColumnDescriptor.push({
        type: 'blank',
        blankSide: 'left',
        width: 0,
        isPlacehoder: true
      });
    }
    // detail 行跳过水平虚拟化，渲染所有列，确保 column 0 的 getSpanRect 能正确执行
    var isDetailRow = (rowProps === null || rowProps === void 0 ? void 0 : rowProps['data-row-detail-key']) != null;
    var rowDescriptors = isDetailRow ? _mapInstanceProperty(_context2 = flat.full).call(_context2, function (col, i) {
      return {
        type: 'normal',
        col: col,
        colIndex: i
      };
    }) : visibleColumnDescriptor;
    return /*#__PURE__*/React.createElement("tr", _extends({}, rowProps, {
      className: rowClass,
      key: (rowProps === null || rowProps === void 0 ? void 0 : rowProps['data-row-detail-key']) ? rowProps['data-row-detail-key'] : internals.safeGetRowKey(primaryKey, record, rowIndex),
      "data-rowindex": rowIndex,
      "data-role": 'table-row'
    }), _mapInstanceProperty(rowDescriptors).call(rowDescriptors, function (descriptor) {
      if (descriptor.type === 'blank') {
        return /*#__PURE__*/React.createElement("td", {
          key: descriptor.blankSide,
          style: {
            visibility: descriptor.isPlacehoder ? 'hidden' : undefined
          }
        });
      }
      return renderBodyCell(record, rowIndex, descriptor.col, descriptor.colIndex, isDetailRow ? fullFlatCount : undefined);
    }));
  }
  function renderBodyCell(record, rowIndex, column, colIndex, colSpanLimit) {
    var _a, _b, _c;
    if (spanManager.testSkip(rowIndex, colIndex)) {
      return null;
    }
    var value = 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, (colSpanLimit !== null && colSpanLimit !== void 0 ? colSpanLimit : 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);
      if (direction === 'rtl') {
        positionStyle.right = hozInfo.stickyLeftMap.get(colIndex);
      } else {
        positionStyle.left = hozInfo.stickyLeftMap.get(colIndex);
      }
    } else if (colIndex >= fullFlatCount - rightFlatCount) {
      positionStyle.position = 'sticky';
      if (direction === 'rtl') {
        positionStyle.left = hozInfo.stickyRightMap.get(colIndex) + (typeof stickyRightOffset === 'number' ? stickyRightOffset : 0);
      } else {
        positionStyle.right = hozInfo.stickyRightMap.get(colIndex) + (typeof stickyRightOffset === 'number' ? stickyRightOffset : 0);
      }
    }
    return /*#__PURE__*/React.createElement('td', _extends(_extends(_extends(_extends({
      key: colIndex
    }, cellProps), {
      className: cx(Classes.tableCell, cellProps.className, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Classes.first, colIndex === 0), Classes.last, colIndex + colSpan === fullFlatCount), Classes.lockLeft, colIndex < leftFlatCount || tbodyPosition === 'left'), Classes.lockRight, colIndex >= fullFlatCount - rightFlatCount), Classes.rowSpan, rowSpan > 1))
    }), hasSpan ? {
      colSpan: colSpan,
      rowSpan: rowSpan
    } : null), {
      style: _extends(_extends({
        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);
  }
}