import { ReportData } from "../../features/report"; import { mergeBucketPeriods, getAmount, PeriodKey, } from "../report.helpers"; import { ChartDataPoint } from "./HistoryChart.models"; // ─── Tab → PeriodKey ───────────────────────────────────────── const TAB_TO_KEY: Record = { Daily: "daily", Weekly: "weekly", Monthly: "monthly", "All Time": "all", }; export function tabToKey(tab: string): PeriodKey { return TAB_TO_KEY[tab] ?? "all"; } // ─── Comparison ────────────────────────────────────────────── function attachComparison( points: ChartDataPoint[], key: PeriodKey ): ChartDataPoint[] { const getCompareIndex = (i: number) => { if (key === "daily") return i - 7; if (key === "weekly") return i - 4; if (key === "monthly") return i - 12; return -1; }; return points.map((p, i) => { const ci = getCompareIndex(i); return { ...p, compare: ci >= 0 && points[ci] ? { id: points[ci].id, label: points[ci].label, amount: points[ci].amount, } : undefined, }; }); } // ─── Main adapter ──────────────────────────────────────────── export function buildChartData( reportData: ReportData, key: PeriodKey, flow: "outflows" | "inflows", comparison: boolean ): ChartDataPoint[] { const merged = mergeBucketPeriods(reportData.buckets, key); let points: ChartDataPoint[] = merged.map((p) => ({ id: p.id, label: p.label, amount: getAmount(p), })); if (comparison) { points = attachComparison(points, key); } return points; }