/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var get = require('lodash/get');
var log = require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.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) {
  const rowValue = get__default["default"](row, rowKey);
  if (rowValue === void 0) log.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 = VueCompositionAPI.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) => {
    var _skipSpansMap$value;
    (_skipSpansMap$value = skipSpansMap.value) === null || _skipSpansMap$value === void 0 ? void 0 : _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 === null || onTrRowspanOrColspan === void 0 ? void 0 : onTrRowspanOrColspan(params, state);
      }
    }
  };
  VueCompositionAPI.watch(() => [data.value, columns.value, rowspanAndColspan], () => {
    updateSkipSpansMap(data.value, columns.value, rowspanAndColspan === null || rowspanAndColspan === void 0 ? void 0 : rowspanAndColspan.value);
  }, {
    immediate: true
  });
  return {
    skipSpansMap
  };
}

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