-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathupsertMapWaypointsInOperationalPoints.ts
54 lines (45 loc) · 1.69 KB
/
upsertMapWaypointsInOperationalPoints.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-disable import/prefer-default-export */
import type { TFunction } from 'i18next';
import type { PathfindingResultSuccess, TrainScheduleResult } from 'common/api/osrdEditoastApi';
import type { OperationalPoint } from '../types';
/**
* Check if the train path used waypoints added by map click and add them to the operational points
*/
export const upsertMapWaypointsInOperationalPoints = (
path: TrainScheduleResult['path'],
pathItemsPositions: PathfindingResultSuccess['path_item_positions'],
operationalPoints: OperationalPoint[],
t: TFunction
): OperationalPoint[] => {
let waypointCounter = 1;
return path.reduce(
(operationalPointsWithAllWaypoints, step, i) => {
if (!('track' in step)) return operationalPointsWithAllWaypoints;
const positionOnPath = pathItemsPositions[i];
const indexToInsert = operationalPointsWithAllWaypoints.findIndex(
(op) => op.position >= positionOnPath
);
const formattedStep: OperationalPoint = {
id: step.id,
extensions: {
identifier: {
name: t('requestedPoint', { count: waypointCounter }),
uic: 0,
},
},
part: { track: step.track, position: step.offset },
position: positionOnPath,
weight: null,
};
waypointCounter += 1;
// If we can't find any op position greater than the current step position, we add it at the end
if (indexToInsert === -1) {
operationalPointsWithAllWaypoints.push(formattedStep);
} else {
operationalPointsWithAllWaypoints.splice(indexToInsert, 0, formattedStep);
}
return operationalPointsWithAllWaypoints;
},
[...operationalPoints]
);
};