import { AnnotationGeneratorStrategyAbstract } from './annotation-generator-strategy.abstract';
/** Handle text style annotation */
export class TextStyleAnnotationHandler extends AnnotationGeneratorStrategyAbstract {
  /**
   * Can be handle.
   *
   * @param {RTEDtoClassPrototype} targetDto - Target DTO.
   * @param {HtmlElementToAnnotationGenerateOptions} options - Options for annotation generator.
   * @returns {boolean} - .
   */
  canHandle(targetDto, options) {
    return !!options.autoMatchFormat && targetDto.name === 'RTETextBlock';
  }
  /**
   * Handle annotation.
   *
   * @param {HTMLElement} element - Current elements.
   * @returns {VegaRTETextBlockAnnotations} - Annotation.
   */
  handle(element) {
    const textStyle = this.getTextStyle(element);
    return textStyle ? { textStyle: textStyle } : {};
  }
  /**
   * Get the node type of element. The element type is determined in the following
   * order: "data-type" attribute, node name, font size and class name.
   *
   * @param {HTMLElement} element - element.
   * @returns {Nullable<VegaRTETextStyleType>} - node type.
   */
  getTextStyle(element) {
    const byDataType = this.getElementTypeByDataType(element);
    if (byDataType)
      return byDataType;
    const byNodeName = this.getElementTypeByNodeName(element);
    if (byNodeName)
      return byNodeName;
    const byFontSize = this.getElementTypeByFontSize(element);
    if (byFontSize)
      return byFontSize;
    const byClassName = this.getElementTypeByClassName(element);
    if (byClassName)
      return byClassName;
    return null;
  }
  /**
   * Get the element type by data-type attribute.
   *
   * @param {HTMLElement} element - element.
   * @returns {Nullable<VegaRTETextStyleType>} - element type or null if not found.
   */
  getElementTypeByDataType(element) {
    if (element.nodeType !== Node.TEXT_NODE && element.hasAttribute('data-type')) {
      return element.getAttribute('data-type');
    }
    return null;
  }
  /**
   * Get the element type by font size.
   *
   * @param {HTMLElement} element - element.
   * @returns {Nullable<VegaRTETextStyleType>} - element type or null if not found.
   */
  getElementTypeByFontSize(element) {
    var _a;
    switch ((_a = element.style) === null || _a === void 0 ? void 0 : _a.fontSize) {
      case '36px':
        return 'title';
      case '28px':
        return 'subtitle';
      case '22px':
        return 'heading-1';
      case '20px':
        return 'heading-2';
      case '18px':
        return 'heading-3';
      default:
        return null;
    }
  }
  /**
   * Get the element type by node name.
   *
   * @param {HTMLElement} element - element.
   * @returns {Nullable<VegaRTETextStyleType>} - element type or null if not found.
   */
  getElementTypeByNodeName(element) {
    switch (element.nodeName) {
      case 'P':
        return 'paragraph';
      case 'H1':
        return 'heading-1';
      case 'H2':
        return 'heading-2';
      case 'H3':
        return 'heading-3';
      case 'H4':
        return 'heading-4';
      case 'H5':
        return 'heading-5';
      case 'H6':
        return 'heading-6';
      default:
        return null;
    }
  }
  /**
   * Get the element type by class name.
   *
   * @param {HTMLElement} element - element.
   * @returns {Nullable<VegaRTETextStyleType>} - element type or null if not found.
   */
  getElementTypeByClassName(element) {
    if (element.className) {
      // Match class name with pattern "v-rte--text-style-{textStyle}" or "v-rte-{textStyle}".
      const match = /\bv-rte-(?:-text-style-)?(title|subtitle|heading-1|heading-2|heading-3|heading-4|heading-5|heading-6|paragraph)\b/.exec(element.className);
      if (match) {
        return match[1];
      }
    }
    return null;
  }
}
