import { generateUUID } from '../../../../utils/misc';
import { ActionHandleStrategy } from '../../dto/action-handle-strategies/action-handle-strategy.abstract';
import { InsertNodeToNearestRootAction } from '../../dto/actions/insert-node-to-nearest-root-action';
import { CustomAttributeAnnotation } from '../../dto/annotations/custom-attribute-annotation';
import { RTETokenNode } from './token-node';
/**
 * Strategy to handle the insertion of a token node into the nearest root block.
 */
export class InsertTokenNodeStrategy extends ActionHandleStrategy {
  /**
   * @inheritDoc
   */
  handleAction(action, block) {
    const { rteRange, token } = action;
    if (rteRange && rteRange.startNode) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      const { key, label, customAttrs = { wrapped: 'true' } } = token;
      const tokenNode = new RTETokenNode(generateUUID(), key, label, block);
      tokenNode.annotationMap.set('CUSTOM_ATTRIBUTE', new CustomAttributeAnnotation(customAttrs));
      block.apply(new InsertNodeToNearestRootAction(rteRange, tokenNode));
      action.actionHandleResult = true;
    }
  }
}
