/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { ref, watch } from '@vue/composition-api';
import get from 'lodash/get';
import '../../../chunks/index.js';
import { l as log } from '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-fe796db6.js';

function getCellKey(row, rowKey, colKey, colIndex) {
  const rowValue = get(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) {
  const skipSpansMap = ref( /* @__PURE__ */new Map());
  const onTrRowspanOrColspan = (params, skipSpansValue) => {
    const {
      rowIndex,
      colIndex
    } = params;
    if (!skipSpansValue.rowspan && !skipSpansValue.colspan) return;
    const maxRowIndex = rowIndex + (skipSpansValue.rowspan || 1);
    const maxColIndex = colIndex + (skipSpansValue.colspan || 1);
    for (let i = rowIndex; i < maxRowIndex; i++) {
      for (let j = colIndex; j < maxColIndex; j++) {
        if (i !== rowIndex || j !== colIndex) {
          if (!data.value[i] || !columns.value[j]) return;
          const cellKey = getCellKey(data.value[i], rowKey.value, columns.value[j].colKey, j);
          const state = skipSpansMap.value.get(cellKey) || {};
          state.skipped = true;
          skipSpansMap.value.set(cellKey, state);
        }
      }
    }
  };
  const updateSkipSpansMap = (data2, columns2, rowspanAndColspan2) => {
    skipSpansMap.value?.clear();
    if (!data2 || !rowspanAndColspan2) return;
    for (let i = 0, len = data2.length; i < len; i++) {
      const row = data2[i];
      for (let j = 0, colLen = columns2.length; j < colLen; j++) {
        const col = columns2[j];
        const params = {
          row,
          col,
          rowIndex: i,
          colIndex: j
        };
        const cellKey = getCellKey(row, rowKey.value, col.colKey, j);
        const state = skipSpansMap.value.get(cellKey) || {};
        const 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?.(params, state);
      }
    }
  };
  watch(() => [data.value, columns.value, rowspanAndColspan], () => {
    updateSkipSpansMap(data.value, columns.value, rowspanAndColspan?.value);
  }, {
    immediate: true
  });
  return {
    skipSpansMap
  };
}

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