UNPKG

@geoapify/route-planner-sdk

Version:

TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js

40 lines (39 loc) 1.79 kB
import { InvalidInsertionPosition } from "../../../../../models"; /** * Shared helper for resolving insert positions */ export class InsertPositionResolver { static hasExplicitInsertPosition(options) { return ((options.afterId !== undefined && options.afterId !== '') || options.afterWaypointIndex !== undefined) && options.append === true; } static shouldAppend(options) { return options.append === true && !options.afterId && options.afterWaypointIndex === undefined; } static resolveInsertPosition(options) { if (options.afterWaypointIndex !== undefined) { return options.afterWaypointIndex + 1; } return 0; } static validateAfterWaypointIndex(context, agentIndex, waypointIndex) { const waypoints = context.getAgentWaypoints(agentIndex); const lastWaypointIndex = waypoints.length - 1; if (waypoints.length === 0) { throw new InvalidInsertionPosition(`Agent ${agentIndex} has no route.`, agentIndex); } if (waypointIndex < 0 || waypointIndex >= waypoints.length) { throw new InvalidInsertionPosition(`Waypoint index ${waypointIndex} out of range (0-${waypoints.length - 1})`, agentIndex, waypointIndex); } if (waypointIndex === lastWaypointIndex && waypoints[lastWaypointIndex].actions.some(action => action.type === 'end')) { throw new InvalidInsertionPosition(`Cannot change the route after waypoint ${waypointIndex} (end location).`, agentIndex, waypointIndex); } } static extractRouteLocations(agentFeature) { return agentFeature.properties.waypoints .map(waypoint => waypoint.location || waypoint.original_location); } }