import { ActionHandlerInterceptor, } from '../../../dto/action-handle-strategies/action-handle-interceptor.abstract';
import { InsertNewParagraphAction } from '../../../dto/actions/insert-new-paragraph-action';
import { RTEFunctionBlock } from '../dto/function-block';
/**
 * Prevent special type input in function block.
 */
export class PreventNewParagraphInterceptor extends ActionHandlerInterceptor {
  /**
   * Checks the target block parent whether is function block.
   *
   * @param {InsertNewParagraphAction} _action - the action to be intercepted
   * @param {RTETextBlock} target - the target block
   * @returns {boolean} - true if the target block parent is function block, false otherwise
   */
  canIntercept(_action, target) {
    return target.parent && target.parent instanceof RTEFunctionBlock;
  }
  /**
   * Insert a new paragraph behind of the function block instead of the target block.
   *
   * @param {ActionHandlerExecution} _execution - the original execution function
   * @param {ModifyContentAction} _action - the action to be intercepted
   * @param {RTETextBlock} target - the target block
   */
  intercept(_execution, _action, target) {
    const insertParagraphAction = new InsertNewParagraphAction(target.children[0], 0);
    insertParagraphAction.isFlushable = true;
    target.parent.apply(insertParagraphAction);
  }
}
