import { type FormEvent, useState, useMemo } from 'react' import type { BossBattle, CreateBossResultInput } from '../types/game' import { ConditionBadge } from './ConditionBadge' interface BossDefeatModalProps { boss: BossBattle onSubmit: (data: CreateBossResultInput) => void onClose: () => void isPending?: boolean hardcoreMode?: boolean starterName?: string | null } function matchVariant(labels: string[], starterName?: string | null): string | null { if (!starterName || labels.length === 0) return null const lower = starterName.toLowerCase() const matches = labels.filter((l) => l.toLowerCase().includes(lower)) return matches.length === 1 ? (matches[0] ?? null) : null } export function BossDefeatModal({ boss, onSubmit, onClose, isPending, hardcoreMode, starterName, }: BossDefeatModalProps) { const [result, setResult] = useState<'won' | 'lost'>('won') const [attempts, setAttempts] = useState('1') const variantLabels = useMemo(() => { const labels = new Set() for (const bp of boss.pokemon) { if (bp.conditionLabel) labels.add(bp.conditionLabel) } return [...labels].sort() }, [boss.pokemon]) const hasVariants = variantLabels.length > 0 const autoMatch = useMemo( () => matchVariant(variantLabels, starterName), [variantLabels, starterName] ) const showPills = hasVariants && autoMatch === null const [selectedVariant, setSelectedVariant] = useState( autoMatch ?? (hasVariants ? (variantLabels[0] ?? null) : null) ) const displayedPokemon = useMemo(() => { if (!hasVariants) return boss.pokemon return boss.pokemon.filter( (bp) => bp.conditionLabel === selectedVariant || bp.conditionLabel === null ) }, [boss.pokemon, hasVariants, selectedVariant]) const handleSubmit = (e: FormEvent) => { e.preventDefault() onSubmit({ bossBattleId: boss.id, result: hardcoreMode ? 'won' : result, attempts: hardcoreMode ? 1 : Number(attempts) || 1, }) } return (

Battle: {boss.name}

{boss.location}

{/* Boss team preview */} {boss.pokemon.length > 0 && (
{showPills && (
{variantLabels.map((label) => ( ))}
)}
{[...displayedPokemon] .sort((a, b) => a.order - b.order) .map((bp) => (
{bp.pokemon.spriteUrl ? ( {bp.pokemon.name} ) : (
)} {bp.pokemon.name} Lv.{bp.level}
))}
)}
{!hardcoreMode && ( )}
{!hardcoreMode && (
setAttempts(e.target.value)} className="w-full px-3 py-2 border rounded-md bg-surface-2 border-border-default" />
)}
) }