import { LinkGroupAnnotation } from '../../annotations/link-group-annotation';
import { RTEFilterStylesStrategy } from '../filter-styles-strategy.abstract';
import { Predicate } from '../predicate';
/**
 * The LinkFilterStylesStrategy is responsible for filtering link styles from text nodes that are annotated as links.
 */
export class LinkFilterStylesStrategy extends RTEFilterStylesStrategy {
  constructor() {
    super(...arguments);
    this.stylesToBeFiltered = LinkGroupAnnotation.getDefaultStyles();
    this.elementTagName = 'a';
  }
  /**
   * Filter default link styles from `link.customStyle` in annotations.
   *
   * @param {VegaRTEAnnotations} annotations - The annotations to filter.
   * @returns {VegaRTEAnnotations} The same annotations reference with link.customStyle cleaned up.
   */
  filterAnnotations(annotations) {
    const link = annotations.link;
    if (!(link === null || link === void 0 ? void 0 : link.customStyle)) {
      return annotations;
    }
    this.extractAndDeleteMatchedStyles(link.customStyle);
    if (Object.keys(link.customStyle).length === 0) {
      delete link.customStyle;
    }
    return annotations;
  }
  /**
   * @inheritDoc
   */
  canFilter(blockOrNodeInstance) {
    return Predicate.isLinkNode(blockOrNodeInstance);
  }
  /**
   * @inheritDoc
   */
  canRefill(blockOrNodeClassName, element) {
    return blockOrNodeClassName === 'RTETextNode' && element.matches(this.elementTagName);
  }
  /**
   * @inheritDoc
   */
  toCssText() {
    return this.buildCssTextForStyles(this.elementTagName, this.classNameToBeAdded);
  }
  /**
   * Filters the inline styles for a link group annotation and updates the attributes accordingly.
   *
   * @param {Record<string, string | AnnotationStyle>} attributes The attributes of the link group annotation.
   * @returns {Record<string, string | AnnotationStyle>} The updated attributes after filtering.
   */
  filterLinkGroupInlineStyles(attributes) {
    const fullStyles = attributes.style;
    const classNames = attributes.class ? attributes.class.split(' ') : [];
    const filterResult = this.filterAndDeleteStyles(fullStyles, classNames);
    filterResult.styles &&
      Object.keys(filterResult.styles).length > 0 &&
      (attributes.style = filterResult.styles);
    filterResult.classNames &&
      filterResult.classNames.length > 0 &&
      (attributes.class = filterResult.classNames.join(' '));
    if (attributes.style && Object.keys(attributes.style).length === 0) {
      delete attributes.style;
    }
    return attributes;
  }
}
