import { MessageSquare, Send } from 'lucide-react' import { useId, useState } from 'react' import { askProductQuestion } from '../../api/client' import { Button, Card, CardBody, Spinner } from '../ui' const SUGGESTIONS = ['What makes it this grade?', 'Is it high in salt?'] /** Session chat. Defaults are the food-label ones; the prescription page passes its own. */ export default function Chat({ sessionId, disabledNote, ask: askApi = askProductQuestion, title = 'Ask about this product', suggestions = SUGGESTIONS, inputLabel = 'Your question about this product', footer = 'Answers use only this product’s label and database data', }) { const [question, setQuestion] = useState('') const [history, setHistory] = useState([]) const [busy, setBusy] = useState(false) const inputId = useId() async function ask(q) { q = q.trim() if (!q || busy) return setBusy(true) setQuestion('') try { const r = await askApi(sessionId, q) setHistory((h) => [...h, { q, a: r.answer }]) } catch (err) { setHistory((h) => [...h, { q, a: `That question couldn’t be answered (${err.message}).` }]) } finally { setBusy(false) } } return (

{history.length > 0 && ( )} {disabledNote ? (

{disabledNote}

) : ( history.length === 0 && (
{suggestions.map((s) => ( ))}
) )}
{ e.preventDefault() ask(question) }} > setQuestion(e.target.value)} disabled={busy || Boolean(disabledNote)} className="min-h-11 min-w-0 flex-1 rounded-control border border-control bg-card px-3 text-base text-ink placeholder:text-ink-3 disabled:bg-raised" />

{footer}

) }