export type TopologySelectionCause = "pointer" | "keyboard" | "selection" | "navigation" | "search" | "programmatic"; export type TopologyNavigationKind = "select" | "focus"; export interface TopologyNodeSelectedDetail { nodeId: string; referenceNodeId?: string | null; cause?: TopologySelectionCause; previousId?: string | null; sourceId?: string | null; } export interface TopologyNavigateDetail { fromId?: string | null; toId: string; kind?: TopologyNavigationKind; } export function dispatchTopologyNavigate(target: EventTarget | null, detail: TopologyNavigateDetail) { if (!target) return; target.dispatchEvent(new CustomEvent("topology-navigate", { bubbles: true, detail, })); } export function resolveTopologySelectionKind(detail: TopologyNodeSelectedDetail): TopologyNavigationKind { return detail.cause === "navigation" ? "focus" : "select"; } export function resolveTopologyReferenceNodeId(nodeId: string) { return nodeId; } export function bindTopologyNodeSelected( target: EventTarget | null, handler: (detail: TopologyNodeSelectedDetail) => void, ) { if (!target) return () => {}; const listener = (event: Event) => { const customEvent = event as CustomEvent; const detail = customEvent.detail; if (!detail?.nodeId) return; handler(detail); }; target.addEventListener("topology-node-selected", listener); return () => { target.removeEventListener("topology-node-selected", listener); }; }