import { TextStyleAnnotation } from '../../annotations/text-style-annotation';
import { RTEFilterStylesStrategy } from '../filter-styles-strategy.abstract';
import { Predicate } from '../predicate';
/**
 * Filters the default paragraph styles from customStyle when parsing an
 * RTEListItemBlock.  List-item blocks implicitly use paragraph styling,
 * so these inline defaults are redundant and should not be persisted.
 */
export class ListItemBlockFilterStylesStrategy extends RTEFilterStylesStrategy {
  constructor() {
    super(...arguments);
    this.stylesToBeFiltered = TextStyleAnnotation.getDefaultStyles('paragraph');
    this.elementTagName = 'li';
    this.headingOrTitleClassNamePattern = /^v-rte-(h[1-6]|title|subtitle)$/;
  }
  /**
   * Check the important font-size for list item block are default styles, and the block is a list item block.
   *
   * @inheritDoc
   */
  canFilter(blockOrNodeInstance) {
    const styles = blockOrNodeInstance.getStyles();
    const isDefaultStyles = !styles.fontSize ||
      this.isBlockOrNodeStylesMatchFilterStyles(blockOrNodeInstance, ['fontSize', 'lineHeight']);
    return Predicate.isListItemBlock(blockOrNodeInstance.dtoName) && isDefaultStyles;
  }
  /**
   * @inheritDoc
   */
  canRefill(blockOrNodeClassName, element) {
    return (Predicate.isListItemBlock(blockOrNodeClassName) &&
      element.matches(this.elementTagName) &&
      this.isElementNotHeadingOrTitle(element));
  }
  /**
   * @inheritDoc
   */
  toCssText() {
    return this.buildCssTextForStyles(this.elementTagName);
  }
  /**
   * Check the list item element does not have heading or title class names.
   *
   * @param {HTMLElement} element - The list item element to check.
   * @returns {boolean} - True if the element does not have heading or title class names, false otherwise.
   */
  isElementNotHeadingOrTitle(element) {
    const classNames = element.className.split(' ');
    return !classNames.some((className) => this.headingOrTitleClassNamePattern.test(className.trim()));
  }
}
