/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var VueCompositionAPI = require('@vue/composition-api');
var get = require('lodash/get');
var _common_js_log_log = require('../../_common/js/log/log.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

function getCellKey(row, rowKey, colKey, colIndex) {
  var rowValue = get__default["default"](row, rowKey);

  if (rowValue === void 0) {
    _common_js_log_log["default"].error("Table", "rowKey is wrong, can not get unique identifier of row.");
  }

  return [rowValue, colKey || colIndex].join("_");
}
function useRowspanAndColspan(data, columns, rowKey, rowspanAndColspan) {
  var skipSpansMap = VueCompositionAPI.ref( /* @__PURE__ */new Map());

  var onTrRowspanOrColspan = function onTrRowspanOrColspan(params, skipSpansValue) {
    var rowIndex = params.rowIndex,
        colIndex = params.colIndex;
    if (!skipSpansValue.rowspan && !skipSpansValue.colspan) return;
    var maxRowIndex = rowIndex + (skipSpansValue.rowspan || 1);
    var maxColIndex = colIndex + (skipSpansValue.colspan || 1);

    for (var i = rowIndex; i < maxRowIndex; i++) {
      for (var j = colIndex; j < maxColIndex; j++) {
        if (i !== rowIndex || j !== colIndex) {
          if (!data.value[i] || !columns.value[j]) return;
          var cellKey = getCellKey(data.value[i], rowKey.value, columns.value[j].colKey, j);
          var state = skipSpansMap.value.get(cellKey) || {};
          state.skipped = true;
          skipSpansMap.value.set(cellKey, state);
        }
      }
    }
  };

  var updateSkipSpansMap = function updateSkipSpansMap(data2, columns2, rowspanAndColspan2) {
    var _skipSpansMap$value;

    (_skipSpansMap$value = skipSpansMap.value) === null || _skipSpansMap$value === void 0 ? void 0 : _skipSpansMap$value.clear();
    if (!data2 || !rowspanAndColspan2) return;

    for (var i = 0, len = data2.length; i < len; i++) {
      var row = data2[i];

      for (var j = 0, colLen = columns2.length; j < colLen; j++) {
        var col = columns2[j];
        var params = {
          row: row,
          col: col,
          rowIndex: i,
          colIndex: j
        };
        var cellKey = getCellKey(row, rowKey.value, col.colKey, j);
        var state = skipSpansMap.value.get(cellKey) || {};
        var o = rowspanAndColspan2(params) || {};

        if (o.rowspan || o.colspan || state.rowspan || state.colspan) {
          o.rowspan && (state.rowspan = o.rowspan);
          o.colspan && (state.colspan = o.colspan);
          skipSpansMap.value.set(cellKey, state);
        }

        onTrRowspanOrColspan === null || onTrRowspanOrColspan === void 0 ? void 0 : onTrRowspanOrColspan(params, state);
      }
    }
  };

  VueCompositionAPI.watch(function () {
    return [data.value, columns.value, rowspanAndColspan];
  }, function () {
    updateSkipSpansMap(data.value, columns.value, rowspanAndColspan === null || rowspanAndColspan === void 0 ? void 0 : rowspanAndColspan.value);
  }, {
    immediate: true
  });
  return {
    skipSpansMap: skipSpansMap
  };
}

exports["default"] = useRowspanAndColspan;
exports.getCellKey = getCellKey;
//# sourceMappingURL=useRowspanAndColspan.js.map
