import type { MapBox } from "./network-map-layout" type Point = { x: number; y: number } /** Routes IPFIX around cards. A missing route leaves the observation available in service details. */ export function mapObservationRoute(start: Point, end: Point, boxes: readonly MapBox[], usedTracks: readonly number[]) { if (end.x - start.x < 44) return null const padding = 8 const obstacles = boxes.filter(box => box.x + box.w > start.x && box.x < end.x) const first = Math.min(start.x + 20, ...obstacles.map(box => box.x - padding)) const last = Math.max(end.x - 20, ...obstacles.map(box => box.x + box.w + padding)) if (first <= start.x || last >= end.x || last - first < 34) return null const middle = (start.y + end.y) / 2 const candidates = [middle, start.y, end.y, ...obstacles.flatMap(box => [box.y - 16, box.y + box.h + 16])] const legal = candidates.flatMap(y => [y, y - 14, y + 14, y - 28, y + 28]) .filter(y => obstacles.every(box => y < box.y - padding || y > box.y + box.h + padding)) .sort((a, b) => Math.abs(a - middle) - Math.abs(b - middle) || a - b) const y = legal.find(candidate => usedTracks.every(track => Math.abs(candidate - track) >= 18)) if (y === undefined) return null return { d: `M ${start.x} ${start.y} H ${first} V ${y} H ${last} V ${end.y} H ${end.x}`, marker: { x: (first + last) / 2, y }, track: y, } }