1
|
// Business logic for tracking tool
|
2
|
|
3
|
import { CatalogItemDto, PathDto } from '../../swagger/data-contracts'
|
4
|
|
5
|
// For more comprehensive code alias CatalogItemDto[] as path variant
|
6
|
export type PathVariant = MapPoint[]
|
7
|
|
8
|
export class MapPoint {
|
9
|
constructor(
|
10
|
public idx: number,
|
11
|
public active: boolean,
|
12
|
public catalogItem: CatalogItemDto
|
13
|
) { }
|
14
|
|
15
|
/**
|
16
|
* @returns true if the map point is displayable - i.e. it has a valid lat/lng
|
17
|
*/
|
18
|
get displayable() {
|
19
|
return !!this.catalogItem.latitude && !!this.catalogItem.longitude
|
20
|
}
|
21
|
}
|
22
|
|
23
|
/**
|
24
|
* Cartesian product of two arrays
|
25
|
* @param sets
|
26
|
* @returns
|
27
|
*/
|
28
|
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
|
29
|
sets.reduce<CatalogItemDto[][]>(
|
30
|
(results, ids) =>
|
31
|
results
|
32
|
.map((result) => ids.map((id) => [...result, id]))
|
33
|
.reduce((nested, result) => [...nested, ...result]),
|
34
|
[[]]
|
35
|
)
|
36
|
|
37
|
/**
|
38
|
* Builds a list of all possible path variants from pathDto
|
39
|
* @param pathDto
|
40
|
* @returns
|
41
|
*/
|
42
|
export const buildPathVariants = (pathDto: PathDto): PathVariant[] => {
|
43
|
if (!pathDto.foundCatalogItems) {
|
44
|
return []
|
45
|
}
|
46
|
|
47
|
return (
|
48
|
pathDto.foundCatalogItems.length === 1
|
49
|
? pathDto.foundCatalogItems
|
50
|
: cartesianProduct(pathDto.foundCatalogItems)
|
51
|
).map((variant, _) =>
|
52
|
variant.map(
|
53
|
(catalogItem, idx) =>
|
54
|
new MapPoint(
|
55
|
idx,
|
56
|
!!catalogItem.latitude && !!catalogItem.longitude,
|
57
|
catalogItem
|
58
|
)
|
59
|
)
|
60
|
)
|
61
|
}
|
62
|
|
63
|
export default buildPathVariants
|