import { type FormEvent, useMemo, useState } 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 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, starterName, }: BossDefeatModalProps) { 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: 'won', 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}
))}
)}
) }