"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _internals = require("../internals");

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

var _warnTransformsDeprecated = require("./warnTransformsDeprecated");

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


function makeAutoRowSpanTransform() {
  (0, _warnTransformsDeprecated.warnTransformsDeprecated)('makeAutoRowSpanTransform');
  return (0, _utils.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 (!(0, _utils.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.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 (0, _extends2.default)((0, _extends2.default)({}, col), {
      getSpanRect: function getSpanRect(value, row, rowIndex) {
        return spanRects[rowIndex];
      }
    });
  });
}