/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { ref, watch } from 'vue';
import { g as get_1 } from '../../_chunks/dep-4c3e8dd5.mjs';
import '../../_common/js/log/index.mjs';
import log from '../../_common/js/log/log.mjs';
import '../../_chunks/dep-a0b4a044.mjs';
import '../../_chunks/dep-7acc11cb.mjs';
import '../../_chunks/dep-e3b14d70.mjs';
import '../../_chunks/dep-66d6733b.mjs';
import '../../_chunks/dep-b4041200.mjs';
import '../../_chunks/dep-be27d184.mjs';
import '../../_chunks/dep-c959a6fb.mjs';
import '../../_chunks/dep-c31e1d9e.mjs';
import '../../_chunks/dep-0bfbf721.mjs';
import '../../_chunks/dep-39cc276d.mjs';
import '../../_chunks/dep-6b22dd27.mjs';
import '../../_chunks/dep-0f0c89ff.mjs';
import '../../_chunks/dep-8f777748.mjs';
import '../../_chunks/dep-ae357f26.mjs';
import '../../_chunks/dep-37ba476d.mjs';

function getCellKey(row, rowKey, colKey, colIndex) {
  var rowValue = get_1(row, rowKey);
  if (rowValue === void 0) log.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 = 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);
      }
    }
  };
  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
  };
}

export { useRowspanAndColspan as default, getCellKey };
//# sourceMappingURL=useRowspanAndColspan.mjs.map
