import { ActionHandleStrategy } from '../../../dto/action-handle-strategies/action-handle-strategy.abstract';
import { InsertChildrenBeforeAction } from '../../../dto/actions/insert-children-before-block';
import { RTETableCellBlock } from '../dto/table-cell-block';
import { RTETableHeadCellBlock } from '../dto/table-head-cell-block';
import { createNewCell, setSpanAttributes } from '../utils/utils';
/**
 * Strategy to create a split segment table cell.
 */
export class CreateSegmentTableCellStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, row) {
    const { table, sourceCell, colSpan, cellType, keepSourceContent, } = action.payload;
    const segmentCell = keepSourceContent
      ? this.convertCellType(sourceCell, cellType)
      : createNewCell(cellType);
    setSpanAttributes(segmentCell, colSpan, sourceCell.rowSpan, table);
    row.apply(new InsertChildrenBeforeAction(sourceCell, segmentCell));
  }
  /**
   * Convert a cell to the requested table cell type.
   *
   * @param {RTETableCellBlock | RTETableHeadCellBlock} cell - Cell to convert.
   * @param {string} targetCellType - Requested cell type.
   * @returns {RTETableCellBlock | RTETableHeadCellBlock} Converted cell.
   */
  convertCellType(cell, targetCellType) {
    return targetCellType === 'table-head-cell'
      ? RTETableHeadCellBlock.from(cell.toJSON())
      : RTETableCellBlock.from(cell.toJSON());
  }
}
