'use client'; import { useId, useState } from 'react'; import type { AllocationState } from '@/game/ecaz-spice-allocation'; import { territory } from '@/game/board'; import { Button } from './ui/button'; import { Input } from './ui/input'; const buttonClass = 'min-h-11 whitespace-normal motion-reduce:transition-none'; /** Parent key: event + allocation.index + allocation.player. A new lot or * negotiating turn starts with a fresh draft rather than an old proposal. */ export function EcazSpice({ event, allocation, me, players, send, busy, }: { event: string; allocation: AllocationState; me: string; players: readonly { id: string; name: string }[]; send: (action: { type: string; [key: string]: unknown }) => void; busy: boolean; }) { const id = useId(); const lot = allocation.lots[allocation.index]; const [draft, setDraft] = useState(() => String(Math.floor((lot?.amount ?? 0) / 2)), ); if (!lot || allocation.player !== me) return null; const name = (player: string) => players.find((p) => p.id === player)?.name ?? player; const ecazName = name(lot.ecaz); const allyName = name(lot.ally); const ecazShare = Number(draft); const valid = draft.trim() !== '' && Number.isSafeInteger(ecazShare) && ecazShare >= 0 && ecazShare <= lot.amount; const equalEcaz = Math.floor(lot.amount / 2); const equalAlly = lot.amount - equalEcaz; const offer = allocation.offer; return (

Share spice from {territory(lot.territory).name}

Divide this shared pool of {lot.amount} spice between{' '} {ecazName} (Ecaz{me === lot.ecaz ? ', you' : ''}) and {allyName} (ally {me === lot.ally ? ', you' : ''}). This choice applies only to the shared spice collected here.

{offer && ( {name(offer.by)} proposes {offer.ecazShare} spice for {ecazName} and{' '} {lot.amount - offer.ecazShare} for {allyName}. Accept this split or propose another. )}
{ e.preventDefault(); if (busy || !valid) return; send({ type: 'decision', event, allocation: { kind: 'propose', ecazShare }, }); }} >

{valid ? `Proposed split: ${ecazShare} for ${ecazName}, ${lot.amount - ecazShare} for ${allyName}.` : `Enter a whole number from 0 to ${lot.amount}.`}

{offer && ( )}

Equal fallback: {equalEcaz} spice for {ecazName}, {equalAlly} for{' '} {allyName}. The ally receives any odd remainder.

); }