import { useState, useMemo } from 'react' import type { EncounterDetail, UpdateEncounterInput, CreateEncounterInput } from '../types' import { useEvolutions, useForms } from '../hooks/useEncounters' import { TypeBadge } from './TypeBadge' import { formatEvolutionMethod } from '../utils/formatEvolution' interface StatusChangeModalProps { encounter: EncounterDetail onUpdate: (data: { id: number data: UpdateEncounterInput }) => void onClose: () => void isPending: boolean region?: string onCreateEncounter?: (data: CreateEncounterInput) => void } export function StatusChangeModal({ encounter, onUpdate, onClose, isPending, region, onCreateEncounter, }: StatusChangeModalProps) { const { pokemon, currentPokemon, route, nickname, catchLevel, faintLevel, deathCause } = encounter const isDead = faintLevel !== null const displayPokemon = currentPokemon ?? pokemon const [showConfirm, setShowConfirm] = useState(false) const [showEvolve, setShowEvolve] = useState(false) const [showFormChange, setShowFormChange] = useState(false) const [showShedConfirm, setShowShedConfirm] = useState(false) const [pendingEvolutionId, setPendingEvolutionId] = useState(null) const [shedNickname, setShedNickname] = useState('') const [deathLevel, setDeathLevel] = useState('') const [cause, setCause] = useState('') const activePokemonId = currentPokemon?.id ?? pokemon.id const { data: evolutions, isLoading: evolutionsLoading } = useEvolutions( showEvolve || showShedConfirm ? activePokemonId : null, region, ) const { data: forms } = useForms(isDead ? null : activePokemonId) const { normalEvolutions, shedCompanion } = useMemo(() => { if (!evolutions) return { normalEvolutions: [], shedCompanion: null } return { normalEvolutions: evolutions.filter(e => e.trigger !== 'shed'), shedCompanion: evolutions.find(e => e.trigger === 'shed') ?? null, } }, [evolutions]) const handleConfirmDeath = () => { onUpdate({ id: encounter.id, data: { faintLevel: deathLevel ? Number(deathLevel) : undefined, deathCause: cause || undefined, }, }) } const handleEvolve = (toPokemonId: number) => { if (shedCompanion && onCreateEncounter) { setPendingEvolutionId(toPokemonId) setShowEvolve(false) setShowShedConfirm(true) return } onUpdate({ id: encounter.id, data: { currentPokemonId: toPokemonId }, }) } const applyEvolution = (includeShed: boolean) => { if (pendingEvolutionId === null) return onUpdate({ id: encounter.id, data: { currentPokemonId: pendingEvolutionId }, }) if (includeShed && shedCompanion && onCreateEncounter) { onCreateEncounter({ routeId: encounter.routeId, pokemonId: shedCompanion.toPokemon.id, nickname: shedNickname || undefined, status: 'caught', origin: 'shed_evolution', }) } } return (
{/* Header */}

{isDead ? 'Death Details' : 'Pokemon Status'}

{/* Pokemon info */}
{displayPokemon.spriteUrl ? ( {displayPokemon.name} ) : (
{displayPokemon.name[0].toUpperCase()}
)}
{nickname || displayPokemon.name}
{nickname && (
{displayPokemon.name}
)}
{displayPokemon.types.map((type) => ( ))}
Lv. {catchLevel ?? '?'} · {route.name}
{currentPokemon && (
Originally: {pokemon.name}
)}
{/* Dead pokemon: view-only details */} {isDead && (
Deceased
{faintLevel !== null && (
Level at death: {' '} {faintLevel}
)} {deathCause && (
Cause: {' '} {deathCause}
)}
)} {/* Alive pokemon: actions */} {!isDead && !showConfirm && !showEvolve && !showFormChange && !showShedConfirm && (
{forms && forms.length > 0 && ( )}
)} {/* Evolution selection */} {!isDead && showEvolve && (

Evolve into:

{evolutionsLoading && (

Loading evolutions...

)} {!evolutionsLoading && normalEvolutions.length === 0 && (

No evolutions available

)} {!evolutionsLoading && normalEvolutions.length > 0 && (
{normalEvolutions.map((evo) => ( ))}
)}
)} {/* Shed evolution confirmation (Nincada → Ninjask + Shedinja) */} {!isDead && showShedConfirm && shedCompanion && (

Shed Evolution

{shedCompanion.toPokemon.spriteUrl ? ( {shedCompanion.toPokemon.name} ) : (
{shedCompanion.toPokemon.name[0].toUpperCase()}
)}

{displayPokemon.name} shed its shell! Would you also like to add{' '} {shedCompanion.toPokemon.name}?

setShedNickname(e.target.value)} placeholder={shedCompanion.toPokemon.name} className="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-amber-500" />
)} {/* Form change selection */} {!isDead && showFormChange && (

Change form to:

{forms && forms.length > 0 && (
{forms.map((form) => ( ))}
)}
)} {/* Confirmation form */} {!isDead && showConfirm && (

This cannot be undone (Nuzlocke rules).

setDeathLevel(e.target.value)} placeholder="Level" className="w-24 px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-red-500" />
setCause(e.target.value)} placeholder="e.g. Crit from rival's Charizard" className="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-red-500" />
)}
{/* Footer for dead/no-confirm/no-evolve views */} {(isDead || (!isDead && !showConfirm && !showEvolve && !showFormChange && !showShedConfirm)) && (
)}
) }