'use client'; import Link from 'next/link'; import { useTranslations } from 'next-intl'; import { useState, useTransition } from 'react'; import { Button } from '@/components/ui/button'; import { swapSlotRecipe, type CurrentPlan, type PlanSlot } from './actions'; const MEAL_ORDER: PlanSlot['meal'][] = ['COLAZIONE', 'PRANZO', 'SPUNTINO', 'CENA']; const DAY_KEYS = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', ] as const; const DAY_NUMERAL = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']; export function PlanWeek({ plan }: { plan: CurrentPlan }) { const t = useTranslations('Plan'); const byDay = new Map(); for (const slot of plan.slots) { if (!byDay.has(slot.dayOfWeek)) byDay.set(slot.dayOfWeek, []); byDay.get(slot.dayOfWeek)!.push(slot); } return (
{DAY_KEYS.map((dayKey, idx) => { const slots = (byDay.get(idx) ?? []).sort( (a, b) => MEAL_ORDER.indexOf(a.meal) - MEAL_ORDER.indexOf(b.meal), ); const dayKcal = slots.reduce((sum, s) => sum + (s.selected?.kcal ?? 0), 0); // Nessuno slot → giorno di digiuno completo (ESE_24). const isFastingDay = slots.length === 0; // Solo CENA con dailyKcal ridotto → giorno "leggero" del 5:2. const isLightDay = !isFastingDay && slots.length === 1 && slots[0].meal === 'CENA'; return (
{DAY_NUMERAL[idx]}

{t(`days.${dayKey}`)} {isFastingDay ? ( · {t('fastingDay')} ) : isLightDay ? ( · {t('lightDay')} ) : null}

{isFastingDay ? '— kcal' : t('dayTotal', { kcal: Math.round(dayKcal) })}
{isFastingDay ? (

{t('fastingDayHint')}

) : (
{slots.map((slot, slotIdx) => ( ))}
)}
); })}
); } const MEAL_ROMAN = ['I', 'II', 'III', 'IV']; function SlotCard({ slot, index }: { slot: PlanSlot; index: number }) { const t = useTranslations('Plan'); const [open, setOpen] = useState(false); const [pending, startTransition] = useTransition(); function swap(recipeId: string) { startTransition(async () => { await swapSlotRecipe(slot.id, recipeId); setOpen(false); }); } return (

{MEAL_ROMAN[index]} {t(`meals.${slot.meal}`)}

{slot.selected ? ( {slot.selected.name} ) : (

{t('noRecipe')}

)} {slot.selected ? (

{Math.round(slot.selected.kcal)}{' '} kcal

) : null} {slot.alternatives.length > 0 ? (
{open ? (
    {slot.alternatives.map((alt) => (
  • {alt.name}{' '} {Math.round(alt.kcal)} kcal
  • ))}
) : null}
) : null}
); }