import type { Node, NodeId } from '@astrale-os/sdk/graph/node' import type { NodePath } from '@astrale-os/sdk/graph/path' import type { Key } from '@astrale-os/sdk/schema' import { Path } from '@astrale-os/sdk/graph/path' import type { ReadState } from '../graph/memory/read/envelope.js' import type { SerializedLocation } from './history.js' export interface PointResult extends ReadState { readonly node: Node | null } /** One canonical location and its current caller-visible point projection. */ export interface NavLocation { readonly target: NodePath | null readonly id: NodeId | null readonly node: Node | null readonly view?: Key.View readonly params: Readonly> readonly missing: boolean } const EMPTY_PARAMS: Readonly> = Object.freeze({}) export const IDLE_LOCATION: NavLocation = Object.freeze({ target: null, id: null, node: null, params: EMPTY_PARAMS, missing: false, }) export function buildLocation( entry: SerializedLocation | null, result: PointResult | null, ): NavLocation { if (entry === null) return IDLE_LOCATION const node = result?.node ?? null return { target: entry.node, id: node?.id ?? idAnchor(entry.node), node, ...(entry.view === undefined ? {} : { view: entry.view }), params: entry.params ?? EMPTY_PARAMS, missing: result !== null && !result.pending && node === null, } } function idAnchor(target: NodePath): NodeId | null { const path = Path.from(target) return path.ast.anchor.kind === 'id' && path.ast.steps.length === 0 ? path.ast.anchor.id : null } export type { Node, NodeId }