import { InternalAnnotationTypeEnum } from '../../annotations/annotation.abstract';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Split the link nodes in the corresponding link group
 */
export class LinkGroupNodeSplitStrategy extends ActionHandleStrategy {
  /**
   * Split the text node.
   *
   * @param {LinkGroupNodeSplitAction} action - The split action.
   * @param {RTETextNode} target - The text node that need to be splitted.
   */
  handleAction(action, target) {
    const linkGroupAnnotation = target.parent.getAnnotationByType(InternalAnnotationTypeEnum.LINK_GROUP);
    if (linkGroupAnnotation) {
      const currentGroupKey = target.annotationMap.get('LINK').link
        .groupKey;
      const currentLinkGroup = linkGroupAnnotation.linkGroups.get(currentGroupKey);
      if (currentLinkGroup) {
        const index = currentLinkGroup.indexOf(target);
        if (index !== -1) {
          /* istanbul ignore next */
          currentLinkGroup.splice(index, 1, ...(Array.isArray(action.splittedTextNodes)
            ? action.splittedTextNodes
            : [action.splittedTextNodes]));
          linkGroupAnnotation.linkGroups.set(currentGroupKey, currentLinkGroup);
        }
      }
    }
  }
}
