import { VegaRTEContent } from '../../../dto/content-state';
import { ActionHandleStrategy } from '../../../dto/action-handle-strategies/action-handle-strategy.abstract';
import { generateUUID } from '../../../../../utils/misc';
import { InsertChildrenAfterAction } from '../../../dto/actions/insert-children-after-block';
import { AppendChildrenAction } from '../../../dto/actions/append-child-nodes-action';
import { RTEFunctionBlock } from '../dto/function-block';
/**
 * Strategy to handle the insertion of a function block into the rich text editor.
 */
export class InsertFunctionBlockStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, target) {
    const { functionType } = action;
    const functionBlock = this.getFunctionBlock(functionType);
    const emptyParagraph = this.createNewParagraph('');
    const referenceBlock = this.getReferenceBlock(target);
    if (referenceBlock) {
      referenceBlock.parent.apply(new InsertChildrenAfterAction(referenceBlock, functionBlock, emptyParagraph));
    }
    else {
      target.apply(new AppendChildrenAction([functionBlock, emptyParagraph]));
    }
    if (emptyParagraph) {
      action.nextFocusNode = emptyParagraph.children[0];
    }
  }
  /**
   * Get a function block based on the specified type.
   *
   * @param {string} type - The type of function block to create.
   * @returns {RTEFunctionBlock} The function block instance.
   */
  getFunctionBlock(type) {
    const id = generateUUID();
    return RTEFunctionBlock.from({
      id: id,
      type: 'function-block',
      children: [
        {
          id: `${id}-1`,
          type: 'logic-control-block',
          statementText: type.toUpperCase(),
        },
        {
          id: `${id}-2`,
          annotations: {
            customClass: ['statementelement'],
          },
          type: 'paragraph',
          nodes: [
            {
              id: `${id}-2-1`,
              type: 'text',
              text: '   ',
            },
          ],
        },
        {
          id: `${id}-3`,
          type: 'logic-control-block',
          statementText: 'SHOW',
        },
        {
          id: `${id}-4`,
          annotations: {
            customClass: ['statementelement'],
          },
          type: 'paragraph',
          nodes: [
            {
              id: `${id}-4-1`,
              type: 'text',
              text: '   ',
            },
          ],
        },
      ],
    });
  }
  /**
   * We need to get the correct block if the current target is a function block element to avoid nesting function blocks.
   *
   * @param {RTEBlock | VegaRTEContent} currentTarget - The current target block or content.
   * @returns {Nullable<RTEBlock>} The reference block.
   */
  getReferenceBlock(currentTarget) {
    if (!(currentTarget instanceof VegaRTEContent)) {
      const parentBlock = currentTarget.parent;
      if (parentBlock instanceof RTEFunctionBlock) {
        return parentBlock;
      }
      else {
        return currentTarget;
      }
    }
  }
}
