"use client";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
interface NutritionData {
energy?: number;
fat?: number;
carbohydrates?: number;
proteins?: number;
sugars?: number;
fiber?: number;
sodium?: number;
}
interface NutritionChartProps {
data: NutritionData;
className?: string;
}
export function NutritionChart({ data, className = "" }: NutritionChartProps) {
const nutritionItems = [
{
key: "energy",
label: "Energia",
value: data.energy,
unit: "kcal",
color: "bg-orange-500",
maxValue: 500,
},
{
key: "fat",
label: "Gorduras",
value: data.fat,
unit: "g",
color: "bg-red-500",
maxValue: 30,
},
{
key: "carbohydrates",
label: "Carboidratos",
value: data.carbohydrates,
unit: "g",
color: "bg-blue-500",
maxValue: 60,
},
{
key: "proteins",
label: "Proteínas",
value: data.proteins,
unit: "g",
color: "bg-green-500",
maxValue: 25,
},
{
key: "sugars",
label: "Açúcares",
value: data.sugars,
unit: "g",
color: "bg-pink-500",
maxValue: 25,
},
{
key: "fiber",
label: "Fibras",
value: data.fiber,
unit: "g",
color: "bg-purple-500",
maxValue: 10,
},
{
key: "sodium",
label: "Sódio",
value: data.sodium,
unit: "mg",
color: "bg-yellow-500",
maxValue: 2,
},
];
const getBarWidth = (value: number, maxValue: number) => {
const percentage = Math.min((value / maxValue) * 100, 100);
return `${percentage}%`;
};
const getBarColor = (value: number, maxValue: number, color: string) => {
const percentage = (value / maxValue) * 100;
if (percentage > 80) return "bg-red-500";
if (percentage > 60) return "bg-orange-500";
if (percentage > 40) return "bg-yellow-500";
return color;
};
return (
Informação Nutricional
{nutritionItems.map((item) => {
if (item.value === undefined || item.value === null) {
return (
{item.label}
N/A
);
}
const barWidth = getBarWidth(item.value, item.maxValue);
const barColor = getBarColor(item.value, item.maxValue, item.color);
return (
{item.label}
{item.value.toFixed(1)}
{item.unit}
0.8
? "border-red-200 text-red-700"
: item.value / item.maxValue > 0.6
? "border-orange-200 text-orange-700"
: "border-green-200 text-green-700"
}`}
>
{((item.value / item.maxValue) * 100).toFixed(0)}%
0{item.unit}
{item.maxValue}
{item.unit}
);
})}
);
}
interface NutritionSummaryProps {
data: NutritionData;
className?: string;
}
export function NutritionSummary({
data,
className = "",
}: NutritionSummaryProps) {
const getNutritionScore = () => {
if (!data.energy || !data.fat || !data.sugars || !data.sodium) {
return null;
}
// Cálculo simplificado do Nutri-Score
let score = 0;
// Pontos negativos
if (data.energy > 335) score += 1;
if (data.energy > 670) score += 2;
if (data.energy > 1005) score += 3;
if (data.energy > 1340) score += 4;
if (data.energy > 1675) score += 5;
if (data.energy > 2010) score += 6;
if (data.energy > 2345) score += 7;
if (data.energy > 2680) score += 8;
if (data.energy > 3015) score += 9;
if (data.energy > 3350) score += 10;
if (data.sugars > 4.5) score += 1;
if (data.sugars > 9) score += 2;
if (data.sugars > 13.5) score += 3;
if (data.sugars > 18) score += 4;
if (data.sugars > 22.5) score += 5;
if (data.sugars > 27) score += 6;
if (data.sugars > 31) score += 7;
if (data.sugars > 36) score += 8;
if (data.sugars > 40) score += 9;
if (data.sugars > 45) score += 10;
if (data.fat > 1) score += 1;
if (data.fat > 2) score += 2;
if (data.fat > 3) score += 3;
if (data.fat > 4) score += 4;
if (data.fat > 5) score += 5;
if (data.fat > 6) score += 6;
if (data.fat > 7) score += 7;
if (data.fat > 8) score += 8;
if (data.fat > 9) score += 9;
if (data.fat > 10) score += 10;
if (data.sodium > 0.3) score += 1;
if (data.sodium > 0.6) score += 2;
if (data.sodium > 0.9) score += 3;
if (data.sodium > 1.2) score += 4;
if (data.sodium > 1.5) score += 5;
if (data.sodium > 1.8) score += 6;
if (data.sodium > 2.1) score += 7;
if (data.sodium > 2.4) score += 8;
if (data.sodium > 2.7) score += 9;
if (data.sodium > 3) score += 10;
// Pontos positivos
if (data.fiber && data.fiber > 0.9) score -= 1;
if (data.fiber && data.fiber > 1.9) score -= 2;
if (data.fiber && data.fiber > 2.8) score -= 3;
if (data.fiber && data.fiber > 3.7) score -= 4;
if (data.fiber && data.fiber > 4.7) score -= 5;
if (data.proteins && data.proteins > 1.6) score -= 1;
if (data.proteins && data.proteins > 3.2) score -= 2;
if (data.proteins && data.proteins > 4.8) score -= 3;
if (data.proteins && data.proteins > 6.4) score -= 4;
if (data.proteins && data.proteins > 8) score -= 5;
// Converter para letra
if (score <= -1) return "A";
if (score <= 2) return "B";
if (score <= 10) return "C";
if (score <= 18) return "D";
return "E";
};
const nutritionScore = getNutritionScore();
return (
Nutri-Score Estimado
{nutritionScore && (
{nutritionScore}
)}
Energia:
{data.energy?.toFixed(0) || "N/A"} kcal
Gorduras:
{data.fat?.toFixed(1) || "N/A"}g
Carboidratos:
{data.carbohydrates?.toFixed(1) || "N/A"}g
Proteínas:
{data.proteins?.toFixed(1) || "N/A"}g
);
}