var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { NodeTypeEnum } from './node.abstract';
import { ImageAnnotation } from '../annotations/image-annotation';
import { ModifyContentActionType } from '../actions/modify-content-action.abstract';
import { NodeAnnotationTypeEnum } from '../annotations/node-annotation.abstract';
import { isNonNullable } from '../../../../types/type-guard';
import { ActionHandleStrategyRegistry } from '../action-handle-strategies/action-handle-strategy-registry';
import { ImageSetAnnotationMapStrategy } from '../action-handle-strategies/apply-annotation-strategies/image-set-annotation-map-strategy';
import { ImageUpdateUrlStrategy } from '../action-handle-strategies/modify-content-strategies/image-update-url-strategy';
import { AnnotationAction } from '../actions/annotation-action.abstract';
import { CommonAnnotation } from '../annotations/common-annotation.abstract';
import { RTEDecoratorNode } from './decorator-node';
/**
 * Image node
 */
export class RTEImageNode extends RTEDecoratorNode {
  constructor(id, url, parentBlock, annotationMap) {
    super(id, annotationMap);
    this.type = NodeTypeEnum.IMAGE;
    this.url = url;
    this.parent = parentBlock;
  }
  /**
   * Converts a VegaRTEImageNode into an RTEImageNode by mapping nodes and creating annotations.
   *
   * @param {VegaRTEImageNode} node - The node to be converted.
   * @param {RTEImageBlock} parentBlock - The parent block
   * @param {VegaRTETransformOptions} options - Optional transformation options.
   * @returns {RTEImageNode} An instance of `RTEImageBlock`
   */
  static from(node, parentBlock, options = { autoMatchFormat: true }) {
    const imageNode = new RTEImageNode(node.id, node.url, parentBlock);
    const autoMatchFormat = !!options.autoMatchFormat;
    const _a = Object.assign({}, node.annotations), { size } = _a, otherAnnotations = __rest(_a, ["size"]);
    const annotations = Object.assign(Object.assign({}, otherAnnotations), { size: size !== null && size !== void 0 ? size : (autoMatchFormat ? 'md' : undefined) });
    imageNode.annotationMap = new Map(Object.keys(annotations)
      .map((type) => {
      if (type === 'size' || type === 'alt') {
        return ImageAnnotation.from(annotations.size, annotations.alt);
      }
      return this.createAnnotationEntity(type, annotations[type]);
    })
      .filter(isNonNullable));
    return imageNode;
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    const annotations = Array.from(this.annotationMap.values())
      .filter((annotation) => annotation instanceof ImageAnnotation || annotation instanceof CommonAnnotation)
      .reduce((record, annotation) => {
      return Object.assign(Object.assign({}, record), annotation.toJSON());
    }, {});
    if (annotations.alt == '')
      delete annotations.alt;
    return Object.assign(Object.assign({ id: this.id }, (Object.keys(annotations).length > 0 ? { annotations } : {})), { type: 'image', url: this.url });
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    return new RTEImageNode(this.id, this.url, parent, super.cloneAnnotations());
  }
  /**
   * @inheritDoc
   */
  toHtml(options) {
    const imageAnnotation = this.getAnnotationByType(NodeAnnotationTypeEnum.IMAGE);
    const attrStr = super.generateAttributeString(options, ...[
      imageAnnotation && imageAnnotation.alt ? `alt="${imageAnnotation.alt}"` : null,
      `src="${this.url}"`,
      imageAnnotation && imageAnnotation.size ? `data-size="${imageAnnotation.size}"` : null,
    ].filter(isNonNullable));
    return `<img${attrStr}>`;
  }
  /**
   * @inheritDoc
   */
  doAnnotationActionApply(action) {
    const strategy = ActionHandleStrategyRegistry.get(AnnotationAction.name, RTEImageNode.name);
    if (strategy) {
      strategy.execute(action, this);
    }
  }
  /**
   * @inheritDoc
   */
  doModifyActionApply(action) {
    ActionHandleStrategyRegistry.executeTheStrategy(action, this);
  }
}
(() => {
  ActionHandleStrategyRegistry.register(ModifyContentActionType.UPDATE_IMAGE_NODE_URL, RTEImageNode.name, new ImageUpdateUrlStrategy());
  ActionHandleStrategyRegistry.register(AnnotationAction.name, RTEImageNode.name, new ImageSetAnnotationMapStrategy());
})();
