/**
 * Comark component setup — registers renderers and the query widget
 * factory when a workspace database becomes available.
 *
 * Called from app.vue's watch on ws.db.
 */
import { registerComarkComponent, registerWidgetFactory } from "@enesis/editor"
import { parseAttrs } from "@enesis/lezer-comark"
import { createQueryRenderer } from "~/lib/comark-renderers/query"
import { QueryWidget } from "~/lib/comark-renderers/query-widget"
import type { GraphService } from "~/services/graph"

export function setupComarkComponents(graph: GraphService) {
  registerComarkComponent("query", createQueryRenderer())

  registerWidgetFactory("query", (rawAttrs: string) => {
    const parsed = parseAttrs(rawAttrs)
    const widget = new QueryWidget({
      type: parsed.type as string | undefined,
      tag: parsed.tag as string | undefined,
      property: parsed.property as string | undefined,
      equals: parsed.equals as string | undefined,
    }, graph)
    return { dom: widget.dom, destroy: () => widget.destroy() }
  })
}
