1
|
import { CatalogItemDto, PathDto } from '../../../swagger/data-contracts'
|
2
|
import generateUuid from '../../../utils/id/uuidGenerator'
|
3
|
import { MapPoint, MapPointType, PathVariant } from '../trackingToolUtils'
|
4
|
|
5
|
|
6
|
/**
|
7
|
* Cartesian product of two arrays
|
8
|
* @param sets
|
9
|
* @returns
|
10
|
*/
|
11
|
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
|
12
|
sets.reduce<CatalogItemDto[][]>(
|
13
|
(results, ids) =>
|
14
|
results
|
15
|
.map((result) => ids.map((id) => [...result, id]))
|
16
|
.reduce((nested, result) => [...nested, ...result]),
|
17
|
[[]]
|
18
|
)
|
19
|
|
20
|
/**
|
21
|
* Builds a list of all possible path variants from pathDto
|
22
|
* @param pathDto
|
23
|
* @returns
|
24
|
*/
|
25
|
export const buildPathVariants = (pathDto: PathDto, mapPointType: MapPointType = MapPointType.LocalCatalog): PathVariant[] => {
|
26
|
if (!pathDto.foundCatalogItems) {
|
27
|
return []
|
28
|
}
|
29
|
|
30
|
return (
|
31
|
pathDto.foundCatalogItems.length === 1
|
32
|
? pathDto.foundCatalogItems
|
33
|
: cartesianProduct(pathDto.foundCatalogItems)
|
34
|
).map((variant, _) =>
|
35
|
variant.map(
|
36
|
(catalogItem, idx) => (
|
37
|
{
|
38
|
id: '',
|
39
|
idx,
|
40
|
reactId: generateUuid(),
|
41
|
addToPath: !!catalogItem.latitude && !!catalogItem.longitude,
|
42
|
catalogItem,
|
43
|
type: mapPointType,
|
44
|
} as MapPoint)
|
45
|
)
|
46
|
)
|
47
|
}
|
48
|
|
49
|
export default buildPathVariants
|