-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_by_zone.ts
More file actions
50 lines (45 loc) · 1.42 KB
/
Copy pathgroup_by_zone.ts
File metadata and controls
50 lines (45 loc) · 1.42 KB
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
// Stage 6: Group + sort.
// Groups placements by zone, sorts each group by (depth_tier ASC, placement_name).
// Inactive placements are dropped. Error-marked placements go to orphans.
// Items whose zone is unknown go to orphans with an unknown_zone diagnostic.
import type { Diagnostics, GroupedPlacements, ScaledPlacement, Zone } from "./types.js";
export function groupByZone(
scaled: ScaledPlacement[],
zones: Zone[],
diagnostics: Diagnostics = [],
): GroupedPlacements {
const zoneIndex = new Map(zones.map((z) => [z.id, z]));
const groups = new Map<string, ScaledPlacement[]>();
for (const z of zones) groups.set(z.id, []);
const orphans: ScaledPlacement[] = [];
for (const p of scaled) {
if (p.active === false) continue;
if (p._error !== undefined) {
orphans.push(p);
continue;
}
const target = groups.get(p.zone);
if (target === undefined) {
diagnostics.push({
stage: "group",
severity: "error",
kind: "unknown_zone",
placement_name: p.placement_name,
zone: p.zone,
});
orphans.push(p);
continue;
}
target.push(p);
}
for (const arr of groups.values()) {
arr.sort((a, b) => {
const ta = a.depth_tier ?? 0;
const tb = b.depth_tier ?? 0;
if (ta !== tb) return ta - tb;
return a.placement_name.localeCompare(b.placement_name);
});
}
void zoneIndex;
return { groups, orphans };
}