import { ActionHandleStrategy } from '../../../dto/action-handle-strategies/action-handle-strategy.abstract';
import { InsertChildrenAfterAction } from '../../../dto/actions/insert-children-after-block';
import { UpdateCursorPositionAction } from '../../../dto/actions/update-cursor-position-action';
/**
 * Insert a new paragraph behind of current function block strategy.
 */
export class FunctionBlockInsertParagraphStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    const newParagraph = this.createNewParagraph('');
    if (newParagraph) {
      target.parent.apply(new InsertChildrenAfterAction(target, newParagraph));
      action.newBlock = newParagraph;
      this.focusToNextLine(newParagraph);
    }
  }
  /**
   * Focus to next paragraph.
   *
   * @param {RTETextBlock} block - The paragraph need to focus.
   */
  focusToNextLine(block) {
    const root = this.getRootContent(block);
    if (root) {
      root.apply(new UpdateCursorPositionAction(block.children[0], 1));
    }
  }
}
