import { InternalAnnotationTypeEnum } from '../../annotations/annotation.abstract';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Update the text block link group annotation strategy.
 */
export class BlockUpdateLinkGroupStrategy extends ActionHandleStrategy {
  /**
   * Update link group after the child link node update.
   *
   * @param {LinkGroupAnnotationAction} action - The link group annotation action instance.
   * @param {RTETextBlock} target - The current text block.
   */
  handleAction(action, target) {
    let linkGroupAnnotation = target.getAnnotationByType(InternalAnnotationTypeEnum.LINK_GROUP);
    if (action.type === 'update_link') {
      if (linkGroupAnnotation) {
        const linkGroup = linkGroupAnnotation.linkGroups.get(action.groupKey);
        if (linkGroup) {
          linkGroupAnnotation.linkGroups.set(action.groupKey, [linkGroup[0]]);
        }
      }
    }
    else {
      if (linkGroupAnnotation) {
        const linkGroup = linkGroupAnnotation.linkGroups.get(action.groupKey) || [];
        linkGroupAnnotation.linkGroups.set(action.groupKey, [...linkGroup, ...action.nodes]);
      }
      else {
        linkGroupAnnotation = action.toAnnotation();
        linkGroupAnnotation.linkGroups.set(action.groupKey, [...action.nodes]);
      }
      target.annotationMap.set(InternalAnnotationTypeEnum.LINK_GROUP, linkGroupAnnotation);
    }
  }
}
