|
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 = CatalogItemDto[]
|
|
7 |
|
|
8 |
const getPathItemVariants = (
|
|
9 |
catalogItems: CatalogItemDto[][],
|
|
10 |
currentIdx: number,
|
|
11 |
itemIdx: number
|
|
12 |
): PathVariant[] => {
|
|
13 |
if (currentIdx < 0) {
|
|
14 |
return []
|
|
15 |
}
|
|
16 |
|
|
17 |
const variants: PathVariant[] = []
|
|
18 |
const nextIdx = currentIdx - 1
|
|
19 |
const currentItem = catalogItems[currentIdx][itemIdx]
|
|
20 |
for (let i = 0; i < catalogItems[nextIdx].length; i += 1) {
|
|
21 |
const itemPaths = getPathItemVariants(catalogItems, nextIdx, i)
|
|
22 |
// Push current item at the end
|
|
23 |
itemPaths.forEach((variant) => {
|
|
24 |
variant.push(currentItem)
|
|
25 |
})
|
|
26 |
}
|
|
27 |
|
|
28 |
return variants
|
|
29 |
}
|
|
30 |
|
|
31 |
const buildPathVariants = (pathDto: PathDto) => {
|
|
32 |
const catalogItems = pathDto.foundCatalogItems
|
|
33 |
const pathVariants: PathVariant[] = []
|
|
34 |
// If there are no items or list is empty return
|
|
35 |
if (!catalogItems || catalogItems.length === 0) {
|
|
36 |
return []
|
|
37 |
}
|
|
38 |
|
|
39 |
if (catalogItems.length === 1) {
|
|
40 |
return catalogItems[0]
|
|
41 |
}
|
|
42 |
|
|
43 |
// Get items in the last idx since we start from the end
|
|
44 |
const currentIdx = catalogItems.length - 1
|
|
45 |
// For each of the items get all path variants
|
|
46 |
for (let i = 0; i < catalogItems[currentIdx].length; i += 1) {
|
|
47 |
pathVariants.concat(getPathItemVariants(catalogItems, currentIdx, i))
|
|
48 |
}
|
|
49 |
|
|
50 |
return pathVariants
|
|
51 |
}
|
|
52 |
|
|
53 |
export default buildPathVariants
|
path building algorithm implementation
re #9629