/**
 * This class is responsible for keeping track of the DOM elements and their corresponding entities.
 */
class StateEntityRenderingRegistry {
  constructor() {
    this.domToEntity = new WeakMap();
    this.entityToDOM = new WeakMap();
    this.contentEntityToRootDom = new Map();
    this.rootDomToContentEntity = new WeakMap();
  }
  /**
   * Registers a DOM element and its corresponding entity.
   *
   * @param {HTMLElement} dom The DOM element to register.
   * @param {RTEBlock | RTENode | VegaRTEContent} entity The entity to register.
   */
  register(dom, entity) {
    if (!this.isVegaRTEContent(entity)) {
      this.domToEntity.set(dom, entity);
      this.entityToDOM.set(entity, dom);
    }
    else {
      this.registerRootDomAndContentEntity(dom, entity);
    }
  }
  /**
   * Retrieves the entity corresponding to a DOM element.
   *
   * @param {HTMLElement} dom The DOM element to retrieve the entity for.
   * @returns {Nullable<RTEBlock | RTENode>} The entity corresponding to the DOM element, or null if not found.
   */
  getEntityByDOM(dom) {
    return this.domToEntity.get(dom);
  }
  /**
   * Retrieves the DOM element corresponding to an entity.
   *
   * @param {RTEBlock | RTENode} entity The entity to retrieve the DOM element for.
   * @returns {Nullable<HTMLElement>} The DOM element corresponding to the entity, or null if not found.
   */
  getDOMByEntity(entity) {
    if (!this.isVegaRTEContent(entity)) {
      return this.entityToDOM.get(entity);
    }
    else {
      return this.contentEntityToRootDom.get(entity);
    }
  }
  /**
   * Delete the dirty element to entity ref.
   *
   * @example Convert text node to the link node, the old span will change to the link wrapper element, and there will be a new text span, so we need remove the old span ref from the map.
   * @param {HTMLElement} dom - The dirty element ref.
   */
  removeDirtyDomRef(dom) {
    this.domToEntity.delete(dom);
  }
  /**
   * Bind the root dom ref and content entity.
   *
   * @param {HTMLElement} dom - The vega rich text editor ref.
   * @param {VegaRTEContent} contentEntity - The rich text editor value content.
   */
  registerRootDomAndContentEntity(dom, contentEntity) {
    const oldContent = this.rootDomToContentEntity.get(dom);
    if (oldContent && oldContent !== contentEntity) {
      this.contentEntityToRootDom.delete(oldContent);
    }
    this.rootDomToContentEntity.set(dom, contentEntity);
    this.contentEntityToRootDom.set(contentEntity, dom);
  }
  /**
   * Check the entity is VegaRTEContent instance.
   *
   * @param {VegaRTEContent | RTEBlock | RTENode} entity - The entity to check.
   * @returns {entity is VegaRTEContent} - True if the entity is a VegaRTEContent instance, false otherwise.
   */
  isVegaRTEContent(entity) {
    return entity && entity.constructor.name === 'VegaRTEContent';
  }
}
export default new StateEntityRenderingRegistry();
