export const createEmptyTable = (rows = 3, cols = 3) => {
  return Array(rows).fill(null).map(() => Array(cols).fill(''));
};

export const addTableRow = (tableData, rowIndex = null) => {
  const newRow = Array(tableData[0]?.length || 1).fill('');
  const newData = [...tableData];
  if (rowIndex === null) {
    newData.push(newRow);
  } else {
    newData.splice(rowIndex + 1, 0, newRow);
  }
  return newData;
};

export const removeTableRow = (tableData, rowIndex) => {
  if (tableData.length <= 1) return tableData;
  const newData = [...tableData];
  newData.splice(rowIndex, 1);
  return newData;
};

export const addTableColumn = (tableData, colIndex = null) => {
  return tableData.map(row => {
    const newRow = [...row];
    if (colIndex === null) {
      newRow.push('');
    } else {
      newRow.splice(colIndex + 1, 0, '');
    }
    return newRow;
  });
};

export const removeTableColumn = (tableData, colIndex) => {
  if (tableData[0]?.length <= 1) return tableData;
  return tableData.map(row => {
    const newRow = [...row];
    newRow.splice(colIndex, 1);
    return newRow;
  });
};

export const updateTableCell = (tableData, rowIndex, colIndex, content) => {
  const newData = [...tableData];
  newData[rowIndex] = [...newData[rowIndex]];
  newData[rowIndex][colIndex] = content;
  return newData;
};