import { rgbToHex } from '../../../../../../../utils/ui';
import { RTE_DEFAULT_TEXT_COLOR, RTE_TEXT_COLORS } from '../../../../../constants/constant';
import { AnnotationGeneratorStrategyAbstract } from './annotation-generator-strategy.abstract';
/** Handle color annotation */
export class ColorAnnotationHandler extends AnnotationGeneratorStrategyAbstract {
  /**
   * @inheritDoc
   */
  canHandle(targetDto) {
    return targetDto.dtoName === 'RTETextNode';
  }
  /**
   * Handle annotation.
   *
   * @param {HTMLElement} element - Current elements.
   * @param {VegaRTETextAnnotations} parentAnnotations - Parent annotations.
   * @returns {VegaRTETextAnnotations} - Annotation.
   */
  handle(element, parentAnnotations = {}) {
    const color = this.getColor(element) || parentAnnotations.textColor;
    return color ? { textColor: color } : {};
  }
  /**
   * Get current color of element.
   *
   * @param {HTMLElement} element - current element.
   * @returns {Nullable<string>} - color.
   */
  getColor(element) {
    var _a;
    const color = element.style.color;
    const hexColor = (_a = rgbToHex(color)) === null || _a === void 0 ? void 0 : _a.toUpperCase();
    if (hexColor) {
      // We don't display default text color as inline style, because we defined it in the vega-rich-text-content.scss.
      const colorSchema = RTE_TEXT_COLORS.filter((item) => item.key !== RTE_DEFAULT_TEXT_COLOR.key).find((schema) => schema.light === hexColor || schema.dark === hexColor);
      if (colorSchema)
        return colorSchema.key;
      return null;
    }
    else {
      for (const color of RTE_TEXT_COLORS) {
        if (element.style.color.includes(color.key)) {
          return color.key;
        }
      }
      return null;
    }
  }
}
