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';
/**
 * Strategy to handle the paste content into function block.
 */
export class PasteContentStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    if (target.parent) {
      const { blocksToBeInserted, startNode, startOffset } = action;
      let futureFocusBlock = target;
      if (startNode === target.children[0] && startOffset === 0) {
        target.parent.apply(new InsertChildrenBeforeAction(target, ...blocksToBeInserted));
      }
      else {
        const breakAction = new SplitBlockWithNodeAction(startNode, startOffset);
        target.apply(breakAction);
        target.parent.apply(new InsertChildrenAfterAction(target, ...blocksToBeInserted));
        futureFocusBlock = breakAction.newBlock;
      }
      action.futureFocusBlock = futureFocusBlock;
    }
  }
}
