import { generateUUID } from '../../../../../../../utils/misc';
import { Predicate } from '../predicate';
import { AnnotationGeneratorStrategyAbstract } from './annotation-generator-strategy.abstract';
import { CustomAttributeAnnotationHandler } from './custom-attribute-annotation-handler';
import { CustomClassAnnotationHandler } from './custom-class-annotation-handler';
import { CustomStyleAnnotationHandler } from './custom-style-annotation-handler';
/** Handle link annotation */
export class LinkAnnotationHandler extends AnnotationGeneratorStrategyAbstract {
  constructor() {
    super(...arguments);
    this.handlers = [
      new CustomAttributeAnnotationHandler(),
      new CustomClassAnnotationHandler(),
      new CustomStyleAnnotationHandler(),
    ];
  }
  /**
   * Can be handle.
   *
   * @param {RTEDtoClassPrototype} targetDto - Target DTO.
   * @returns {boolean} - .
   */
  canHandle(targetDto) {
    return targetDto.dtoName === 'RTETextNode';
  }
  /**
   * Handle annotation.
   *
   * @param {HTMLElement} element - Current elements.
   * @param {VegaRTETextAnnotations} parentAnnotations - Parent annotations.
   * @returns {VegaRTETextAnnotations} - Annotation.
   */
  handle(element, parentAnnotations = {}) {
    if (parentAnnotations.link)
      return { link: parentAnnotations.link };
    return Predicate.isLinkElement(element) ? { link: this.generateLinkAnnotations(element) } : {};
  }
  /**
   * Generate link annotations.
   *
   * @param {HTMLElement} element - Current elements.
   * @returns {VegaRTELink} - link annotations.
   */
  generateLinkAnnotations(element) {
    const annotations = {
      href: element.getAttribute('href') || '',
      groupKey: generateUUID(),
    };
    this.handlers.forEach((handler) => {
      Object.assign(annotations, handler.handle(element));
    });
    return annotations;
  }
}
