"use client"; import Image from "next/image"; import { useCallback, useEffect, useMemo, useState } from "react"; import { formatEuro, type CustomWaffleConfig, type IngredientPickerItem, } from "@/lib/custom-waffle"; import { isUploadAssetPath } from "@/lib/image-url"; import { WaffleMenuImage } from "./WaffleMenuImage"; function IngredientThumb({ src, alt }: { src: string; alt: string }) { if (src.startsWith("http")) { return ( // eslint-disable-next-line @next/next/no-img-element {alt} ); } return ( {alt} ); } export function CustomWaffleCard({ config, ingredients, }: { config: CustomWaffleConfig; ingredients: IngredientPickerItem[]; }) { const [open, setOpen] = useState(false); const [selected, setSelected] = useState>(new Set()); const selectedList = useMemo( () => ingredients.filter((ing) => selected.has(ing.id)), [ingredients, selected] ); const extrasTotal = useMemo( () => selectedList.reduce((sum, ing) => sum + ing.price, 0), [selectedList] ); const total = config.basePrice + extrasTotal; const close = useCallback(() => setOpen(false), []); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") close(); }; document.addEventListener("keydown", onKey); const prev = document.body.style.overflow; document.body.style.overflow = "hidden"; return () => { document.removeEventListener("keydown", onKey); document.body.style.overflow = prev; }; }, [open, close]); function toggle(id: string) { setSelected((prev) => { const next = new Set(prev); if (next.has(id)) next.delete(id); else next.add(id); return next; }); } return ( <>
  • {open ? (
    e.stopPropagation()} >

    {config.title}

    Base {formatEuro(config.basePrice)} — ajoutez vos ingrédients

    {ingredients.length === 0 ? (

    Aucun ingrédient disponible pour le moment.

    ) : (
      {ingredients.map((ing) => { const isOn = selected.has(ing.id); return (
    • ); })}
    )}
    {selectedList.length === 0 ? "Aucun ingrédient sélectionné" : `${selectedList.length} ingrédient${selectedList.length > 1 ? "s" : ""}`} {formatEuro(total)}
    ) : null} ); }