/**
 * Node view registry — lets third-party code register ProseMirror node views
 * without modifying EditorBlock.vue.
 *
 * Usage:
 * ```typescript
 * import { registerNodeView } from "@enesis/editor"
 *
 * registerNodeView("my_block", (node, view, getPos) => new MyNodeView(node, view, getPos))
 * ```
 */

import type { Node } from "prosemirror-model"
import type { NodeView } from "prosemirror-view"

export type NodeViewFactory = (
  node: Node,
  view: Parameters<
    Exclude<
      import("prosemirror-view").EditorProps["nodeViews"],
      undefined
    >[string]
  >[1],
  getPos: () => number,
) => NodeView

const registry = new Map<string, NodeViewFactory>()

export function registerNodeView(name: string, factory: NodeViewFactory): void {
  registry.set(name, factory)
}

export function getRegisteredNodeViews(): Record<string, NodeViewFactory> {
  const result: Record<string, NodeViewFactory> = {}
  for (const [name, factory] of registry) {
    result[name] = factory
  }
  return result
}

export function clearRegisteredNodeViews(): void {
  registry.clear()
}
