import type { RouteNode, FlowSegment } from '@cameleer/design-system'; // Map NodeType strings to RouteNode types function mapNodeType(type: string): RouteNode['type'] { const lower = type?.toLowerCase() || ''; if (lower.includes('from') || lower === 'endpoint') return 'from'; if (lower.includes('to')) return 'to'; if (lower.includes('choice') || lower.includes('when') || lower.includes('otherwise')) return 'choice'; if (lower.includes('error') || lower.includes('dead')) return 'error-handler'; return 'process'; } function mapStatus(status: string | undefined): RouteNode['status'] { if (!status) return 'ok'; const s = status.toUpperCase(); if (s === 'FAILED') return 'fail'; if (s === 'RUNNING') return 'slow'; return 'ok'; } /** * Maps diagram PositionedNodes + execution ProcessorNodes to RouteFlow RouteNode[] format. * Joins on processorId → node.id (node IDs are Camel processor IDs). */ export function mapDiagramToRouteNodes( diagramNodes: Array<{ id?: string; label?: string; type?: string }>, processors: Array<{ processorId?: string; status?: string; durationMs?: number; children?: any[] }> ): RouteNode[] { // Flatten processor tree const flatProcessors: typeof processors = []; function flatten(nodes: typeof processors) { for (const n of nodes) { flatProcessors.push(n); if (n.children) flatten(n.children); } } flatten(processors || []); // Build lookup: processorId → processor const procMap = new Map(); for (const p of flatProcessors) { if (p.processorId) procMap.set(p.processorId, p); } return diagramNodes.map(node => { const proc = procMap.get(node.id ?? ''); return { name: node.label || node.id || '', type: mapNodeType(node.type ?? ''), durationMs: proc?.durationMs ?? 0, status: mapStatus(proc?.status), isBottleneck: false, }; }); } /** * Splits a flat RouteNode[] into FlowSegment[] (main route + error handlers). * Returns the segments and an index map: indexMap[newFlatIndex] = originalIndex. */ export function toFlowSegments(nodes: RouteNode[]): { flows: FlowSegment[]; indexMap: number[] } { const mainIndices: number[] = []; const errorIndices: number[] = []; for (let i = 0; i < nodes.length; i++) { if (nodes[i].type === 'error-handler') { errorIndices.push(i); } else { mainIndices.push(i); } } const flows: FlowSegment[] = [ { label: 'Main Route', nodes: mainIndices.map(i => nodes[i]) }, ]; if (errorIndices.length > 0) { flows.push({ label: 'Error Handler', nodes: errorIndices.map(i => nodes[i]), variant: 'error' }); } const indexMap = [...mainIndices, ...errorIndices]; return { flows, indexMap }; }