import { Annotation, InternalAnnotationTypeEnum } from './annotation.abstract';
import { camelToDashCase } from '../../../../utils/string';
import { LinkFilterStylesStrategy } from '../filter-styles-strategies/text-node/link-filter-styles-strategy';
/**
 * Annotation to represent a link of grouped nodes
 */
export class LinkGroupAnnotation extends Annotation {
  constructor(groupKey) {
    super();
    this.type = InternalAnnotationTypeEnum.LINK_GROUP;
    this.linkGroups = new Map();
    this.linkGroups = new Map([[groupKey, []]]);
  }
  /**
   * Get the default standalone styles applied to a link by Vega's internal style.
   * These are redundant when parsed back from standalone HTML and should be stripped.
   *
   * @returns {AnnotationStyle} The default styles.
   */
  static getDefaultStyles() {
    return {
      color: 'rgba(var(--v-text-link, 19, 98, 226, 1))',
    };
  }
  clone() {
    const clonedAnnotation = new LinkGroupAnnotation('');
    this.linkGroups.forEach((nodes, key) => {
      clonedAnnotation.linkGroups.set(key, nodes.map((node) => node.clone(node.parent)));
    });
    return clonedAnnotation;
  }
  /**
   * Convert the custom style, class and attribute to string which html can used
   *
   * @param {linkAnnotation} linkAnnotation current link annotation which saved custom style, class and attribute.
   * @param {LinkGroupConversionOptions} options - options.
   * @returns {string} - string.
   */
  toString(linkAnnotation, options) {
    let attributes = this.toJsx(linkAnnotation, options);
    if (options === null || options === void 0 ? void 0 : options.filterInlineStyles) {
      attributes =
        LinkGroupAnnotation.linkGroupFilterStylesStrategy.filterLinkGroupInlineStyles(attributes);
    }
    return Object.entries(attributes)
      .map(([key, value]) => {
      const valueString = typeof value === 'string' ? value : this.formatStyleToString(value);
      return `${camelToDashCase(key)}="${valueString}"`;
    })
      .join(' ');
  }
  /**
   * Convert the custom style, class and attribute to JSX attributes
   *
   * @param {linkAnnotation} linkAnnotation current link annotation which saved custom style, class and attribute.
   * @param {RTERenderContext} options - options.
   * @returns {Record<string, string | AnnotationStyle>} JSX used attributes.
   */
  toJsx(linkAnnotation, options) {
    const linkStyle = this.getStyle(linkAnnotation, options);
    const linkClass = this.getClass(linkAnnotation, options);
    const attributes = this.getAttribute(linkAnnotation);
    if (linkClass.length > 0) {
      attributes.class = linkClass.join(' ');
    }
    if (linkStyle && Object.keys(linkStyle).length > 0) {
      attributes.style = linkStyle;
    }
    return attributes;
  }
  /**
   * Get the custom class for the annotation
   *
   * @param {linkAnnotation} linkAnnotation current link annotation which saved custom style, class and attribute.
   * @param {RTERenderContext} options - options.
   * @returns {string[]} Link class
   */
  getClass(linkAnnotation, options) {
    const linkClass = [];
    if (linkAnnotation.link.customClass)
      linkClass.push(...linkAnnotation.link.customClass);
    if (!(options === null || options === void 0 ? void 0 : options.standalone) && linkAnnotation.useInternalStyle)
      linkClass.push('v-rte--link');
    return linkClass;
  }
  /**
   * Get the custom style for the annotation
   *
   * @param {LinkAnnotation} linkAnnotation current link annotation which saved custom style, class and attribute.
   * @param {RTERenderContext} options - options.
   * @returns {AnnotationStyle} Link style
   */
  getStyle(linkAnnotation, options) {
    const linkStyle = {};
    if (linkAnnotation.link.customStyle)
      Object.assign(linkStyle, linkAnnotation.link.customStyle);
    if ((options === null || options === void 0 ? void 0 : options.standalone) && linkAnnotation.useInternalStyle) {
      Object.assign(linkStyle, LinkGroupAnnotation.getDefaultStyles());
    }
    return linkStyle;
  }
  /**
   * Get the custom attribute for the annotation
   *
   * @param {LinkAnnotation} linkAnnotation current link annotation which saved custom style, class and attribute.
   * @returns {Record<string, string>} Link attributes
   */
  getAttribute(linkAnnotation) {
    const { href, customAttribute = {} } = linkAnnotation.link;
    const useInternalStyle = linkAnnotation.useInternalStyle;
    const target = customAttribute.target || (useInternalStyle ? '_blank' : '');
    return Object.assign(Object.assign(Object.assign({}, customAttribute), (href ? { href } : useInternalStyle ? { href } : {})), (target ? { target } : {}));
  }
  /**
   * Convert style to string which can be used to html string.
   *
   * @param {AnnotationStyle} style Style object.
   * @returns {string} Style string
   */
  formatStyleToString(style) {
    return Object.entries(style)
      .map(([key, value]) => `${camelToDashCase(key)}: ${value};`)
      .join(' ');
  }
}
LinkGroupAnnotation.linkGroupFilterStylesStrategy = new LinkFilterStylesStrategy();
