import { VegaRTEExtension } from '../extension.abstract';
import { VegaRTEContent } from '../../dto/content-state';
import { INSERT_FUNCTION_BLOCK } from './actions/insert-function-block-action';
import { InsertFunctionBlockStrategy } from './action-handler-strategies/insert-function-block-strategy';
import { RTETextBlock } from '../../dto/blocks/text-block';
import { RTEImageBlock } from '../../dto/blocks/image-block';
import { RTELogicControlBlock } from './dto/logic-control-block';
import { FunctionToolbarButtonRenderer } from './renderers/function-toolbar-button-renderer';
import { RTELogicControlBlockRenderer } from './renderers/logic-control-block-renderer';
import { DivToLogicControlBlockStrategy } from './element-to-dto-strategies/div-to-logic-control-block-strategy';
import { DivToFunctionBlockStrategy } from './element-to-dto-strategies/div-to-function-block-strategy';
import { DivToFunctionContentStrategy } from './element-to-dto-strategies/div-to-function-content-strategy';
import { RTEFunctionBlock } from './dto/function-block';
import { RTEFunctionBlockRenderer } from './renderers/function-block-renderer';
import { ModifyContentActionType } from '../../dto/actions/modify-content-action.abstract';
import { PreventNewParagraphInterceptor } from './action-handler-interceptor/prevent-new-paragraph-interceptor';
import { FunctionBlockInsertParagraphStrategy } from './action-handler-strategies/function-block-insert-paragraph-strategy';
import { PreventPasteContentInterceptor } from './action-handler-interceptor/prevent-paste-content-interceptor';
import { PASTE_PLAIN_TEXT } from './actions/paste-plain-text-action';
import { PastePlainTextStrategy } from './action-handler-strategies/paste-plain-text-strategy';
const convertLogicControlBlockStrategy = new DivToLogicControlBlockStrategy();
const convertFunctionContentStrategy = new DivToFunctionContentStrategy();
const convertFunctionBlockStrategy = new DivToFunctionBlockStrategy();
/**
 * Function extension class for Vega Rich Text Editor.
 */
export class VegaRTEFunctionExtension extends VegaRTEExtension {
  constructor() {
    super();
    this.toolbarButtonRenderer = new FunctionToolbarButtonRenderer();
    this.registerBlockBasicStrategies(RTELogicControlBlock.name);
    this.registerBlockBasicStrategies(RTEFunctionBlock.name);
    this.preventFunctionBlockDefaultInsertNewParagraph();
    this.registerNode('logic-control-block', RTELogicControlBlock);
    this.registerRenderer('logic-control-block', new RTELogicControlBlockRenderer());
    this.registerNode('function-block', RTEFunctionBlock);
    this.registerRenderer('function-block', new RTEFunctionBlockRenderer());
    this.registerToolbarButtonRenderer('functions', this.toolbarButtonRenderer);
    this.registerElementToBlockDTOClassStrategy(convertLogicControlBlockStrategy);
    this.registerElementToBlockDTOClassStrategy(convertFunctionContentStrategy);
    this.registerElementToBlockDTOClassStrategy(convertFunctionBlockStrategy);
  }
  /**
   * Remove the appen children strategies to prevent default insert new paragraph logic in function block
   */
  preventFunctionBlockDefaultInsertNewParagraph() {
    this.unRegisterStrategy(ModifyContentActionType.INSERT_NEW_PARAGRAPH, RTELogicControlBlock.name);
    this.unRegisterStrategy(ModifyContentActionType.APPEND_CHILDREN, RTEFunctionBlock.name);
    this.unRegisterStrategy(ModifyContentActionType.INSERT_CHILDREN_AFTER, RTEFunctionBlock.name);
    this.unRegisterStrategy(ModifyContentActionType.INSERT_CHILDREN_BEFORE, RTEFunctionBlock.name);
    this.unRegisterStrategy(ModifyContentActionType.PASTE_CONTENT, RTELogicControlBlock.name);
    VegaRTEExtension.registerActionHandleStrategy(RTEFunctionBlock.name, ModifyContentActionType.INSERT_NEW_PARAGRAPH, new FunctionBlockInsertParagraphStrategy());
  }
}
(() => {
  const insertFunctionBlockStrategy = new InsertFunctionBlockStrategy();
  VegaRTEExtension.registerActionHandleStrategy(VegaRTEContent.name, INSERT_FUNCTION_BLOCK, insertFunctionBlockStrategy);
  VegaRTEExtension.registerActionHandleStrategy(RTETextBlock.name, INSERT_FUNCTION_BLOCK, insertFunctionBlockStrategy);
  VegaRTEExtension.registerActionHandleStrategy(RTEImageBlock.name, INSERT_FUNCTION_BLOCK, insertFunctionBlockStrategy);
  VegaRTEExtension.registerActionHandleStrategy(RTELogicControlBlock.name, INSERT_FUNCTION_BLOCK, insertFunctionBlockStrategy);
  VegaRTEExtension.registerActionHandleStrategy(RTETextBlock.name, PASTE_PLAIN_TEXT, new PastePlainTextStrategy());
  VegaRTEExtension.registerActionHandlerInterceptor(RTETextBlock.name, ModifyContentActionType.INSERT_NEW_PARAGRAPH, new PreventNewParagraphInterceptor());
  VegaRTEExtension.registerActionHandlerInterceptor(RTETextBlock.name, ModifyContentActionType.PASTE_CONTENT, new PreventPasteContentInterceptor());
})();
