import _extends from "@babel/runtime-corejs3/helpers/extends";
import { internals } from '../internals';
import { isLeafNode, traverseColumn } from '../utils';
import { warnTransformsDeprecated } from './warnTransformsDeprecated';

function isIdentity(x, y) {
  return x === y;
}
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */


export function makeAutoRowSpanTransform() {
  warnTransformsDeprecated('makeAutoRowSpanTransform');
  return traverseColumn(function (col, _ref) {
    var dataSource = _ref.dataSource,
        range = _ref.range;

    var _a;

    if (!((_a = col.features) === null || _a === void 0 ? void 0 : _a.autoRowSpan)) {
      return col;
    }

    if (!isLeafNode(col)) {
      return col;
    }

    var isFunc = typeof col.features.autoRowSpan === 'function';
    var shouldMergeCell = isFunc ? col.features.autoRowSpan : isIdentity;
    var spanRects = [];
    var lastBottom = 0;
    var prevValue = null;
    var prevRow = null;

    for (var rowIndex = 0; rowIndex < dataSource.length; rowIndex++) {
      var row = dataSource[rowIndex];
      var value = internals.safeGetValue(col, row, rowIndex);

      if (rowIndex === 0 || !shouldMergeCell(prevValue, value, prevRow, row)) {
        var spanRect = {
          top: lastBottom,
          bottom: rowIndex,
          left: range.start,
          right: range.end
        };

        for (var i = lastBottom; i < rowIndex; i++) {
          spanRects.push(spanRect);
        }

        lastBottom = rowIndex;
      }

      prevValue = value;
      prevRow = row;
    }

    for (var _i = lastBottom; _i < dataSource.length; _i++) {
      spanRects.push({
        top: lastBottom,
        bottom: dataSource.length,
        left: range.start,
        right: range.end
      });
    }

    return _extends(_extends({}, col), {
      getSpanRect: function getSpanRect(value, row, rowIndex) {
        return spanRects[rowIndex];
      }
    });
  });
}