import { createHexGrid, getGridBounds } from "./hex.ts"; import { createWorldMapRecordContext, buildWorldMapRecord, type WorldMapOrganization, type WorldMapPerson } from "./worldMapRecords.ts"; import type { TopologyPageConfig } from "./sceneConfig.ts"; interface OrganizationEntryLike { data: { id: string; name: string; shortName?: string; acronym?: string; region?: string; industry?: string; status?: string; summary?: string; mission?: string; peopleIds?: string[]; businessServices?: string[]; infrastructure?: string[]; weakPoints?: string[]; domains?: string[]; tags?: string[]; employees?: number; branches?: number; contextNotes?: string; notes?: string[]; }; } interface PersonEntryLike { data: { id: string; name: string; organizationId?: string; role?: string; department?: string; archetype?: string; favoriteQuestion?: string; summary?: string; strengths?: string[]; weaknesses?: string[]; tags?: string[]; }; } export interface WorldMapPageModel { organizations: WorldMapOrganization[]; people: WorldMapPerson[]; organizationById: Map; peopleById: Map; peopleByOrganization: Map; organizationNodes: Array<{ id: string; label: string; subtitle?: string; q: number; r: number; variant: string; size: string; active: boolean; selected: boolean; }>; personNodes: Array<{ id: string; label: string; subtitle?: string; q: number; r: number; variant: string; size: string; active: boolean; selected: boolean; }>; topologyNodes: Array<{ id: string; q: number; r: number }>; topologyEdges: Array<{ from: string; to: string; kind: string; active: boolean; muted: boolean; }>; selectedId: string; worldRadius: number; worldCells: ReturnType; worldBounds: ReturnType; selectedRecord: ReturnType; worldMapData: { selectedId: string; organizations: Array<{ id: string; name: string; shortName?: string; acronym?: string; region?: string; industry?: string; status?: string; summary?: string; mission?: string; businessServices?: string[]; domains?: string[]; weakPoints?: string[]; tags?: string[]; employees?: number; contextNotes?: string; }>; people: Array<{ id: string; name: string; organizationId?: string; role?: string; department?: string; archetype?: string; favoriteQuestion?: string; summary?: string; strengths?: string[]; weaknesses?: string[]; tags?: string[]; }>; nodes: Array<{ id: string; q: number; r: number }>; relationships: Array<{ from: string; to: string; kind: string; active: boolean; muted: boolean; }>; radius: number; worldBounds: ReturnType; cameraSafeAreaRatio: number; cameraKeyboardPanStep: number; cameraEdgePanThreshold: number; cameraEdgePanSpeed: number; cameraPersistence: TopologyPageConfig["camera"]["persistence"]; }; } function normalizeOrganizations(entries: OrganizationEntryLike[]): WorldMapOrganization[] { return [...entries] .sort((a, b) => a.data.name.localeCompare(b.data.name)) .map((entry) => ({ id: entry.data.id, name: entry.data.name, shortName: entry.data.shortName ?? entry.data.acronym ?? entry.data.name, acronym: entry.data.acronym ?? "", region: entry.data.region ?? "", industry: entry.data.industry ?? "", status: entry.data.status ?? "", summary: entry.data.summary ?? "", mission: entry.data.mission ?? "", peopleIds: entry.data.peopleIds ?? [], businessServices: entry.data.businessServices ?? [], infrastructure: entry.data.infrastructure ?? [], weakPoints: entry.data.weakPoints ?? [], domains: entry.data.domains ?? [], tags: entry.data.tags ?? [], employees: entry.data.employees ?? 0, branches: entry.data.branches ?? 0, contextNotes: entry.data.contextNotes ?? "", notes: entry.data.notes ?? [], })); } function normalizePeople(entries: PersonEntryLike[]): WorldMapPerson[] { return [...entries] .sort((a, b) => a.data.name.localeCompare(b.data.name)) .map((entry) => ({ id: entry.data.id, name: entry.data.name, organizationId: entry.data.organizationId ?? "", role: entry.data.role ?? "", department: entry.data.department ?? "", archetype: entry.data.archetype ?? "", favoriteQuestion: entry.data.favoriteQuestion ?? "", summary: entry.data.summary ?? "", strengths: entry.data.strengths ?? [], weaknesses: entry.data.weaknesses ?? [], tags: entry.data.tags ?? [], })); } function createOrganizationPosition(index: number, organizationColumnSize: number) { const side = index < organizationColumnSize ? -1 : 1; const rowIndex = index % organizationColumnSize; const r = -10 + rowIndex * 10; return { q: side * 3 - r / 2, r, }; } function createPersonPosition( orgPosition: { q: number; r: number }, index: number, siblingCount: number, ) { const centeredOffsets = siblingCount === 1 ? [0] : siblingCount === 2 ? [-1, 1] : siblingCount === 3 ? [-2, 0, 2] : [-3, -1, 1, 3]; const rOffset = centeredOffsets[index] ?? index * 2 - siblingCount + 1; const outwardDirection = orgPosition.q + orgPosition.r / 2 < 0 ? -1 : 1; const qOffset = Math.round(outwardDirection * 8 - rOffset / 2); return { q: orgPosition.q + qOffset, r: orgPosition.r + rOffset, }; } export function createWorldMapPageModel( organizationEntries: OrganizationEntryLike[], personEntries: PersonEntryLike[], profile: Pick, ): WorldMapPageModel { const organizations = normalizeOrganizations(organizationEntries); const people = normalizePeople(personEntries); const worldMapRecordContext = createWorldMapRecordContext(organizations, people); const organizationById = worldMapRecordContext.organizationsById; const peopleById = worldMapRecordContext.peopleById; const peopleByOrganization = worldMapRecordContext.peopleByOrganization; const organizationColumnSize = Math.ceil(organizations.length / 2); const organizationPositions = new Map(); const organizationNodes = organizations.map((organization, index) => { const position = createOrganizationPosition(index, organizationColumnSize); organizationPositions.set(organization.id, position); return { id: organization.id, label: organization.shortName, subtitle: organization.region || organization.industry || organization.status, q: position.q, r: position.r, variant: "anchor", size: "lg", active: index === 0, selected: index === 0, }; }); const personNodes = people.map((person, index) => { const organizationPosition = organizationPositions.get(person.organizationId) ?? createOrganizationPosition(0, organizationColumnSize); const organizationPeople = peopleByOrganization.get(person.organizationId) ?? []; const siblingIndex = organizationPeople.findIndex((entry) => entry.id === person.id); const position = createPersonPosition( organizationPosition, Math.max(0, siblingIndex), Math.max(1, organizationPeople.length), ); return { id: person.id, label: person.name, subtitle: person.role || person.department || person.archetype, q: position.q, r: position.r, variant: "reference", size: "sm", active: index === 0, selected: false, }; }); const topologyNodes = [...organizationNodes, ...personNodes]; const topologyEdges = people .filter((person) => person.organizationId && organizationById.has(person.organizationId)) .map((person) => ({ from: person.organizationId, to: person.id, kind: "contains", active: false, muted: true, })); const selectedId = organizationNodes[0]?.id ?? personNodes[0]?.id ?? ""; const worldRadius = profile.scene.radius; const worldCells = createHexGrid({ radius: worldRadius, columns: profile.scene.columns, rows: profile.scene.rows, originQ: profile.scene.originQ, originR: profile.scene.originR, }); const worldBounds = getGridBounds(worldCells, worldRadius); const selectedRecord = buildWorldMapRecord(selectedId, worldMapRecordContext); const worldMapData = { selectedId, organizations: organizations.map((organization) => ({ id: organization.id, name: organization.name, shortName: organization.shortName, acronym: organization.acronym, region: organization.region, industry: organization.industry, status: organization.status, summary: organization.summary, mission: organization.mission, businessServices: organization.businessServices, domains: organization.domains, weakPoints: organization.weakPoints, tags: organization.tags, employees: organization.employees, contextNotes: organization.contextNotes, })), people: people.map((person) => ({ id: person.id, name: person.name, organizationId: person.organizationId, role: person.role, department: person.department, archetype: person.archetype, favoriteQuestion: person.favoriteQuestion, summary: person.summary, strengths: person.strengths, weaknesses: person.weaknesses, tags: person.tags, })), nodes: topologyNodes.map((node) => ({ id: node.id, q: node.q, r: node.r, })), relationships: topologyEdges, radius: worldRadius, worldBounds, cameraSafeAreaRatio: profile.camera.safeAreaRatio, cameraKeyboardPanStep: profile.camera.keyboardPanStep, cameraEdgePanThreshold: profile.camera.edgePanThreshold, cameraEdgePanSpeed: profile.camera.edgePanSpeed, cameraPersistence: profile.camera.persistence, }; return { organizations, people, organizationById, peopleById, peopleByOrganization, organizationNodes, personNodes, topologyNodes, topologyEdges, selectedId, worldRadius, worldCells, worldBounds, selectedRecord, worldMapData, }; }