import { ActionHandleStrategy } from '../../../dto/action-handle-strategies/action-handle-strategy.abstract';
import { RemoveChildrenAction } from '../../../dto/actions/remove-child-action';
import { UpdateCursorPositionAction } from '../../../dto/actions/update-cursor-position-action';
import { InsertChildrenAfterAction } from '../../../dto/actions/insert-children-after-block';
/**
 * Strategy to handle the deletion of a table block from the parent.
 */
export class DeleteTableBlockStrategy extends ActionHandleStrategy {
  /**
   * Deletes the specified table block and updates the cursor position.
   *
   * @param {DeleteTableBlockAction} action - The action containing the table block to delete.
   */
  handleAction(action) {
    const tableBlock = action.tableBlock;
    const parent = tableBlock.parent;
    const root = super.getRootContent(tableBlock);
    const newParagraph = super.createNewParagraph('');
    parent.apply(new InsertChildrenAfterAction(tableBlock, newParagraph));
    parent.apply(new RemoveChildrenAction(tableBlock));
    if (root) {
      root.apply(new UpdateCursorPositionAction(newParagraph.getLastNode()));
    }
  }
}
