import { InsertChildrenAfterAction } from '../../actions/insert-children-after-block';
import { InsertChildrenBeforeAction } from '../../actions/insert-children-before-block';
import { SplitBlockWithNodeAction } from '../../actions/split-block-with-node-action';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Insert a image block into the text block at special position strategy.
 */
export class BlockInsertImageIntoTextStrategy extends ActionHandleStrategy {
  /**
   * insertImage
   *
   * @param {InsertImageToBlockAction} action - Insert image action instance.
   * @param {RTETextBlock} target - The text block.
   */
  handleAction(action, target) {
    const splitNode = action.splitPointNode;
    const startOffsetOfNode = action.startOffsetOfNode;
    const newImageBlock = action.imageBlockToBeInserted;
    if (this.isCaretPositionAtBlockEnd(target, splitNode, startOffsetOfNode)) {
      target.parent.apply(new InsertChildrenAfterAction(target, newImageBlock));
    }
    else if (this.isCaretPositionAtBlockStart(target, splitNode, startOffsetOfNode)) {
      target.parent.apply(new InsertChildrenBeforeAction(target, newImageBlock));
    }
    else {
      const splitBlockAction = new SplitBlockWithNodeAction(splitNode, startOffsetOfNode);
      target.apply(splitBlockAction);
      target.parent.apply(new InsertChildrenAfterAction(target, newImageBlock));
    }
  }
}
