'use strict';

/**
 * RTEDTORendererManager is a singleton class that manages the registration and retrieval of RTE DTO renderers.
 */
class RTEDTORendererManager {
  constructor() {
    // Register RTE DTO renderers, mapping type strings to their respective renderer instances.
    this.RTE_DTO_RENDERER_MAP = new Map();
    // Register RTE Toolbar Button Renderers, mapping type strings to their respective renderer instances.
    this.RTE_TOOLBAR_BUTTON_RENDERER_MAP = new Map();
    /**
     * Per-host toolbar button renderer map.
     * Stores a host-specific renderer map so that extensions that register during
     * {@link VegaRTEExtension#prepareBeforeLoad} can provide isolated renderer instances
     * for each {@link HTMLVegaRichTextEditorElement}.  At render time this map is checked
     * first; the global singleton map is used as a fallback.
     */
    this.hostToolbarButtonRendererMap = new WeakMap();
  }
  /**
   * Registers a new RTE DTO renderer.
   *
   * @param {string} type - The type identifier for the RTE DTO renderer.
   * @param {VegaRTEBaseRenderer} RTEDTORenderer - The renderer instance for the RTE DTO.
   */
  registerRTEDTORenderer(type, RTEDTORenderer) {
    const existRenderer = this.RTE_DTO_RENDERER_MAP.get(type);
    if (RTEDTORenderer &&
      existRenderer &&
      existRenderer.constructor.name !== RTEDTORenderer.constructor.name) {
      throw new Error(`Renderer for type ${type} is already registered with a different class(${existRenderer.constructor.name}).`);
    }
    this.RTE_DTO_RENDERER_MAP.set(type, RTEDTORenderer);
  }
  /**
   * Registers a new RTE Toolbar Button Renderer.
   *
   * @param {string} type - The type identifier for the RTE Toolbar Button Renderer.
   * @param {VegaRTEToolbarButtonRenderer} RTEToolbarButtonRenderer - The renderer instance for the RTE Toolbar Button.
   */
  registerRTEToolbarButtonRenderer(type, RTEToolbarButtonRenderer) {
    this.RTE_TOOLBAR_BUTTON_RENDERER_MAP.set(type, RTEToolbarButtonRenderer);
  }
  /**
   * Registers a host-specific toolbar button renderer.
   * The per-host renderer takes precedence over the global singleton when the host
   * element is passed to {@link getRTEToolbarButtonRenderer}.
   *
   * @typedef HTMLVegaRichTextEditorElement - The rich text editor host element type.
   * @param {HTMLVegaRichTextEditorElement} host - The rich text editor host element.
   * @param {string} type - The type identifier for the toolbar button renderer.
   * @param {VegaRTEToolbarButtonRenderer} renderer - The renderer instance to register for this host.
   */
  registerRTEToolbarButtonRendererForHost(host, type, renderer) {
    let hostMap = this.hostToolbarButtonRendererMap.get(host);
    if (!hostMap) {
      hostMap = new Map();
      this.hostToolbarButtonRendererMap.set(host, hostMap);
    }
    hostMap.set(type, renderer);
  }
  /**
   * Retrieves the RTE renderer for a given type.
   *
   * @param {string} type - The type identifier for the RTE block.
   * @returns {Nullable<BaseRenderer>} - The renderer instance for the RTE block, or null if not found.
   */
  getRTERenderer(type) {
    return this.RTE_DTO_RENDERER_MAP.get(type);
  }
  /**
   * Retrieves the RTE Toolbar Button Renderer for a given type.
   * When a host element is provided the per-host map registered via
   * {@link registerRTEToolbarButtonRendererForHost} is checked first; the global
   * singleton map is used as a fallback so that extensions that do not override
   * {@link VegaRTEExtension#prepareBeforeLoad} continue to work unchanged.
   *
   * @typedef HTMLVegaRichTextEditorElement - The rich text editor host element type.
   * @param {string} type - The type identifier for the RTE Toolbar Button Renderer.
   * @param {HTMLVegaRichTextEditorElement} [host] - Optional host element for per-host lookup.
   * @returns {Nullable<VegaRTEToolbarButtonRenderer>} - The renderer instance for the RTE Toolbar Button, or null if not found.
   */
  getRTEToolbarButtonRenderer(type, host) {
    var _a, _b;
    if (host) {
      const hostRenderer = (_a = this.hostToolbarButtonRendererMap
        .get(host)) === null || _a === void 0 ? void 0 : _a.get(type);
      if (hostRenderer) {
        return hostRenderer;
      }
    }
    return (_b = this.RTE_TOOLBAR_BUTTON_RENDERER_MAP.get(type)) !== null && _b !== void 0 ? _b : null;
  }
}
const RTEDTORendererManager$1 = new RTEDTORendererManager();

exports.RTEDTORendererManager = RTEDTORendererManager$1;
