| 1 | import type { Translator } from "./i18n"; |
| 2 | |
| 3 | export type DisplayRateBand = "peak" | "off_peak" | "mixed"; |
| 4 | export type AggregatedRateBand = DisplayRateBand | "unknown"; |
| 5 | |
| 6 | function normalize(value: string | undefined): DisplayRateBand | undefined { |
| 7 | if (value === "peak" || value === "off_peak" || value === "mixed") return value; |
| 8 | return undefined; |
| 9 | } |
| 10 | |
| 11 | // Missing/legacy/static quotes intentionally poison an aggregate: once a turn |
| 12 | // contains an unknown band, the UI must not claim that the whole turn was peak |
| 13 | // or off-peak. |
| 14 | function merge(current: AggregatedRateBand | undefined, value: string | undefined): AggregatedRateBand { |
| 15 | const next = normalize(value) ?? "unknown"; |
| 16 | if (!current) return next; |
| 17 | if (current === "unknown" || next === "unknown") return "unknown"; |
| 18 | if (current === next) return current; |
| 19 | return "mixed"; |
| 20 | } |
| 21 | |
| 22 | function label(value: string | undefined, t: Translator): string | undefined { |
| 23 | const band = normalize(value); |
| 24 | return band |
| 25 | ? t(`billing.rateBand.${band === "off_peak" ? "offPeak" : band}` as Parameters<Translator>[0]) |
| 26 | : undefined; |
| 27 | } |
| 28 | |
| 29 | function append(value: string, band: string | undefined, t: Translator): string { |
| 30 | const suffix = label(band, t); |
| 31 | return suffix ? `${value} · ${suffix}` : value; |
| 32 | } |
| 33 | |
| 34 | export { append as appendRateBand, label as rateBandLabel, merge as mergeRateBand, normalize as normalizeRateBand }; |
| 35 |