import { AppendChildrenAction } from '../../actions/append-child-nodes-action';
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 node at the special position of image block.
 */
export class ImageBlockInsertImageStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    const splitNode = action.splitPointNode;
    const startOffsetOfNode = action.startOffsetOfNode;
    const newImageBlock = action.imageBlockToBeInserted;
    const newImageNodes = newImageBlock.children;
    if (target.children.length < 1) {
      target.apply(new AppendChildrenAction(newImageNodes));
    }
    else 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 {
      target.apply(new SplitBlockWithNodeAction(splitNode, startOffsetOfNode));
      target.parent.apply(new InsertChildrenAfterAction(target, newImageBlock));
    }
  }
}
