import { getAllTableRows, buildTableGrid, buildCellStartColumnMap } from '../utils/utils';
import { TableColumnStrategy } from './table-column-strategy.abstract';
/**
 * Strategy to handle the selection of a table column.
 */
export class SelectTableColumnStrategy extends TableColumnStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    const allTableRows = getAllTableRows(target);
    const tableGrid = buildTableGrid(allTableRows);
    const cellStartColMap = buildCellStartColumnMap(tableGrid);
    const [startCellCol, endCellCol] = this.getBoundaryCellColumnRange(action.tableCellSelectRange, allTableRows, tableGrid);
    const allNeedSelectedCells = [];
    const selectedCellIds = new Set();
    allTableRows.forEach((row) => {
      row.children.forEach((cell) => {
        const cellStartCol = cellStartColMap.get(cell.id);
        if (cellStartCol === undefined) {
          return;
        }
        if (cellStartCol >= startCellCol &&
          cellStartCol <= endCellCol &&
          !selectedCellIds.has(cell.id)) {
          selectedCellIds.add(cell.id);
          allNeedSelectedCells.push(cell);
        }
      });
    });
    action.selectCellCallback(allNeedSelectedCells);
  }
}
