import { isArray, isString, isFunction } from "../_utils/is.mjs";
function getColumnData(columns) {
  if (!isArray(columns)) {
    return [];
  }
  return columns.map((item) => {
    if (isString(item)) {
      return {
        key: item,
        label: item
      };
    }
    return {
      ...item
    };
  });
}
function getSkipCell(rowIndex, columnIndex, span) {
  const skip = {};
  const { colspan = 1, rowspan = 1 } = span;
  for (let i = 0; i < rowspan; i++) {
    for (let j = 0; j < colspan; j++) {
      if (i !== 0 || j !== 0) {
        skip[`${rowIndex + i}-${columnIndex + j}`] = true;
      }
    }
  }
  return skip;
}
function getBodyData(columnData, bodyData, cellSpan) {
  if (!bodyData) {
    return [];
  }
  const t = bodyData.length;
  const s = 0;
  const colLenght = columnData.value.length;
  const rlt = [];
  let span = null;
  const skipCell = {};
  const end = Math.min(s + t, bodyData.length);
  for (let r = s; r < end; r += 1) {
    const row = bodyData[r];
    const cols = [];
    for (let c = 0; c < colLenght; c += 1) {
      const col = columnData.value[c];
      if (isFunction(cellSpan)) {
        span = cellSpan(r, c, row, col);
      }
      const cell = {
        value: row[col.key],
        key: col.key
      };
      if (span) {
        const { colspan = 1, rowspan = 1 } = span;
        Object.assign(skipCell, getSkipCell(r, c, span));
        if (colspan > 1) {
          cell.colspan = colspan;
        }
        if (rowspan > 1) {
          cell.rowspan = rowspan;
        }
      }
      if (!skipCell[`${r}-${c}`]) {
        if (c === colLenght - 1) {
          cell.last = true;
        }
        cols.push(cell);
      }
    }
    rlt.push({ key: row.key, data: cols });
  }
  return rlt;
}
export {
  getBodyData,
  getColumnData
};
