"use client"; import { useState, useEffect, useCallback } from "react"; import { useParams, useRouter } from "next/navigation"; import { ArrowLeft, AlertCircle, Star, AlertTriangle } from "lucide-react"; import { Button } from "@/components/ui/button"; import Image from "next/image"; import { searchByBarcode, Product, formatNutritionData, } from "@/lib/openFoodFactsApi"; import Link from "next/link"; import { NutriScoreBadge, NovaBadge } from "@/app/components/NutriScoreBadge"; import { NutritionGrid } from "@/app/components/NutritionBar"; import { ProductSkeleton } from "@/app/components/ProductSkeleton"; export default function ProdutoPage() { const params = useParams(); const router = useRouter(); const barcode = params.barcode as string; const [product, setProduct] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const searchProduct = useCallback(async () => { try { setLoading(true); setError(null); const result = await searchByBarcode(barcode); if (result) { setProduct(result); } else { setError("Produto não encontrado"); } } catch (err) { setError(err instanceof Error ? err.message : "Erro ao buscar produto"); } finally { setLoading(false); } }, [barcode]); useEffect(() => { if (barcode) { searchProduct(); } }, [barcode, searchProduct]); const getProductImageUrl = (product: Product): string | null => { return ( product.image_front_url || product.image_front_small_url || product.image_url || product.image_small_url || null ); }; const getNutritionGrade = (grade?: string) => { if (!grade) return null; const grades = { a: { label: "A", color: "bg-green-500", textColor: "text-green-700", description: "Muito bom", }, b: { label: "B", color: "bg-green-400", textColor: "text-green-700", description: "Bom", }, c: { label: "C", color: "bg-yellow-500", textColor: "text-yellow-700", description: "Regular", }, d: { label: "D", color: "bg-orange-500", textColor: "text-orange-700", description: "Ruim", }, e: { label: "E", color: "bg-red-500", textColor: "text-red-700", description: "Muito ruim", }, }; return grades[grade.toLowerCase() as keyof typeof grades] || null; }; const getNovaGroup = (group?: number) => { if (!group) return null; const groups = { 1: { label: "1", color: "bg-green-500", textColor: "text-green-700", description: "Alimentos não processados ou minimamente processados", }, 2: { label: "2", color: "bg-yellow-500", textColor: "text-yellow-700", description: "Ingredientes culinários processados", }, 3: { label: "3", color: "bg-orange-500", textColor: "text-orange-700", description: "Alimentos processados", }, 4: { label: "4", color: "bg-red-500", textColor: "text-red-700", description: "Alimentos ultraprocessados", }, }; return groups[group as keyof typeof groups] || null; }; if (loading) { return ; } if (error || !product) { return (

Produto não encontrado

{error || "O produto com este código de barras não foi encontrado na base de dados."}

); } const imageUrl = getProductImageUrl(product); const nutritionGrade = getNutritionGrade(product.nutrition_grades); const novaGroup = getNovaGroup(product.nova_group); const nutritionData = formatNutritionData(product); return (
{/* Hero Section com gradiente e animação */}

{product.product_name || "Produto"}

Código: {product.code}

{/* Content */}
{/* Product Image */}
{imageUrl ? ( {product.product_name ) : (

Sem imagem disponível

)}
{/* Basic Info */}

Informações Básicas

{product.brands && (
Marca: {product.brands}
)} {product.categories && (
Categoria: {product.categories}
)} {product.countries && (
Países: {product.countries}
)}
{/* Product Details */}
{/* Nutrition Grades */}
{nutritionGrade && (

Nutri-Score

{nutritionGrade.description}

Classificação nutricional

)} {novaGroup && (

NOVA

Grupo {novaGroup.label}

Nível de processamento

)}
{/* Nutritional Information */} {nutritionData && (
)} {/* Ingredients */} {product.ingredients_text && (

Ingredientes

{product.ingredients_text}

)} {/* Allergens */} {product.allergens_tags && product.allergens_tags.length > 0 && (

Alérgenos

{product.allergens_tags.map((allergen, index) => ( {allergen.replace("en:", "").replace("pt:", "")} ))}
)} {/* Additives */} {product.additives_tags && product.additives_tags.length > 0 && (

Aditivos

{product.additives_tags .slice(0, 10) .map((additive, index) => ( {additive.replace("en:", "").replace("pt:", "")} ))} {product.additives_tags.length > 10 && ( +{product.additives_tags.length - 10} mais )}
)}
); }