"use client"; import { ComposedChart, Line, LineChart, Bar, AreaChart, Area, Cell, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from "recharts"; import { MONTH_NAMES } from "@/lib/data"; import { computeProfit } from "@/lib/profit"; export type ChartView = "lines" | "stack" | "profit"; type Props = { income: (number | null)[]; fixed: (number | null)[]; running: (number | null)[]; view?: ChartView; incomeColor?: string; fixedColor?: string; runningColor?: string; profitColor?: string; }; function readVar(name: string, fallback: string): string { if (typeof window === "undefined") return fallback; const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim(); return v || fallback; } export function MachineLineChart({ income, fixed, running, view = "lines", incomeColor, fixedColor, runningColor, profitColor, }: Props) { const INC = incomeColor ?? readVar("--color-chart-income", "#0090A6"); const FIX = fixedColor ?? readVar("--color-chart-fixed", "#BE851F"); const RUN = runningColor ?? readVar("--color-chart-running", "#C13B4D"); const PROF = profitColor ?? readVar("--color-chart-running", "#D1495B"); const lastActual = Math.max( income.findLastIndex((v) => v != null), fixed.findLastIndex((v) => v != null), running.findLastIndex((v) => v != null), ); const clipped = lastActual >= 0 ? lastActual + 1 : MONTH_NAMES.length; const months = MONTH_NAMES.slice(0, clipped); const data = months.map((m, i) => ({ month: m, income: income[i] ?? null, fixed: fixed[i] ?? null, running: running[i] ?? null, profit: computeProfit({ income: income[i] ?? 0, fixed: fixed[i] ?? 0, running: running[i] ?? 0 }), })); const fmt = (v: number) => new Intl.NumberFormat("en-ZA", { style: "currency", currency: "ZAR", maximumFractionDigits: 0, notation: "compact" }).format(v); if (view === "profit") { return (