import { createTopologyPageController } from "./pageTopologyController.ts"; import { createSceneNavigationController, type SceneNavigationController, type SceneNavigationControllerOptions, } from "./sceneNavigationController.ts"; import { resolveTopologyReferenceNodeId, resolveTopologySelectionKind, type TopologyNavigationKind, type TopologySelectionCause, } from "./selectionContract.ts"; import { resolveRelationshipContext, type ContextRelationship, type RelationshipContext, } from "./relationshipContext.ts"; export interface TopologyContentSelection { entityId: string; referenceNodeId: string; previousId: string | null; kind: TopologyNavigationKind; cause: TopologySelectionCause; relationshipContext: RelationshipContext; } export interface TopologyContentPageControllerOptions { scene: HTMLElement; initialId: string; relationships?: ContextRelationship[]; navigation: Omit< SceneNavigationControllerOptions, "scene" | "selectedId" | "onMiniMapNodeSelect" >; onSelection: (selection: TopologyContentSelection) => void; onNavigationStateChange?: (active: boolean) => void; } export interface TopologyContentPageController { navigation: SceneNavigationController; getSelectedId(): string; select(entityId: string, kind?: TopologyNavigationKind): void; syncTopology( nodes: Array<{ id: string; q: number; r: number }>, relationships: ContextRelationship[], bounds?: Parameters[1], selectedId?: string, ): void; destroy(): void; } export function createTopologyContentPageController({ scene, initialId, relationships = [], navigation: navigationOptions, onSelection, onNavigationStateChange, }: TopologyContentPageControllerOptions): TopologyContentPageController { let activeId = ""; let activeRelationships = relationships; function applySelection( entityId: string, kind: TopologyNavigationKind, cause: TopologySelectionCause, referenceNodeId?: string | null, ) { if (!entityId) return; const previousId = activeId || null; const isSettledNavigation = entityId === activeId && cause === "navigation"; const relationshipContext = resolveRelationshipContext(entityId, activeRelationships); activeId = entityId; navigation.select(entityId); navigation.syncRelationshipContext(relationshipContext); if (cause !== "programmatic") { navigation.ensureVisible(entityId, true); } scene.dispatchEvent(new CustomEvent("topology-relationship-context", { detail: relationshipContext, })); if (!isSettledNavigation) { onSelection({ entityId, referenceNodeId: referenceNodeId ?? resolveTopologyReferenceNodeId(entityId), previousId, kind, cause, relationshipContext, }); } } function select(entityId: string, kind: TopologyNavigationKind = "select") { if (!entityId) return; const sourceId = activeId || null; applySelection(entityId, kind, "selection", resolveTopologyReferenceNodeId(entityId)); page.navigate({ fromId: sourceId, toId: entityId, kind }); } const navigation = createSceneNavigationController({ ...navigationOptions, scene, selectedId: initialId, onMiniMapNodeSelect(entityId) { select(entityId, "select"); }, }); function syncTopology( nodes: Array<{ id: string; q: number; r: number }>, nextRelationships: ContextRelationship[] = [], bounds?: Parameters[1], selectedId = nodes[0]?.id ?? "", ) { activeRelationships = nextRelationships; activeId = selectedId; navigation.setTopology(nodes, bounds); if (selectedId) navigation.select(selectedId); } const page = createTopologyPageController({ scene, onNodeSelected(detail) { applySelection( detail.nodeId, resolveTopologySelectionKind(detail), detail.cause ?? "pointer", detail.referenceNodeId, ); }, onNavigationStateChange, }); applySelection(initialId, "focus", "programmatic", resolveTopologyReferenceNodeId(initialId)); return { navigation, getSelectedId: () => activeId, select, syncTopology, destroy() { page.destroy(); navigation.destroy(); }, }; }