import { InsertChildrenAfterAction } from '../../actions/insert-children-after-block';
import { InsertChildrenBeforeAction } from '../../actions/insert-children-before-block';
import { UpdateCursorPositionAction } from '../../actions/update-cursor-position-action';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Strategy for inserting text into a decorator node. The decorator can not edit, so we insert the text as a new child.
 */
export class InsertTextToDecoratorNodeStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    const { text, offset, decoratorNode } = action;
    const paragraph = this.createNewParagraph(text);
    if (paragraph) {
      const newTextNode = paragraph.children[0];
      if (offset !== 0) {
        target.apply(new InsertChildrenAfterAction(decoratorNode, newTextNode));
      }
      else {
        target.apply(new InsertChildrenBeforeAction(decoratorNode, newTextNode));
      }
      this.setTheCursorPosition(newTextNode);
    }
  }
  /**
   * Set the cursor position to the new text node.
   *
   * @param {RTENode} focusNode - The new text node to focus on.
   */
  setTheCursorPosition(focusNode) {
    const rootContent = this.getRootContent(focusNode.parent);
    if (rootContent) {
      rootContent.apply(new UpdateCursorPositionAction(focusNode));
    }
  }
}
