import { DemandItem, DemandSignal } from '../../lib/demand/types'; interface SupplyGapChartProps { demandSignal: DemandSignal; showTopN?: number; } function formatCategory(category: string): string { return category.replace(/_/g, ' ').replace(/\b\w/g, (l) => l.toUpperCase()); } export default function SupplyGapChart({ demandSignal, showTopN = 10 }: SupplyGapChartProps) { // Sort items by gap (largest gaps first) const sortedItems = [...demandSignal.demandItems] .filter((item) => item.gapKg > 0) .sort((a, b) => b.gapKg - a.gapKg) .slice(0, showTopN); // Find the max for scaling const maxDemand = Math.max(...demandSignal.demandItems.map((item) => item.weeklyDemandKg)); // Calculate overall stats const totalDemand = demandSignal.totalWeeklyDemandKg; const totalSupply = demandSignal.currentSupplyKg; const totalGap = demandSignal.supplyGapKg; const coveragePercentage = totalDemand > 0 ? ((totalSupply / totalDemand) * 100) : 100; if (demandSignal.demandItems.length === 0) { return (
No demand data available.
{demandSignal.region.name} - {demandSignal.seasonalPeriod}
{/* Overall summary */}{totalDemand.toFixed(0)}
Weekly Demand (kg)
{totalSupply.toFixed(0)}
Current Supply (kg)
0 ? 'text-red-600' : 'text-green-600'}`}> {totalGap.toFixed(0)}
Gap (kg)
= 100 ? 'text-green-600' : coveragePercentage >= 80 ? 'text-yellow-600' : 'text-red-600' }`}> {coveragePercentage.toFixed(0)}%
Coverage