'use client' import { useState } from 'react' import { CharacterSelect, type CharOption } from '@/components/CharacterSelect' import { Turnstile } from '@/components/Turnstile' const ITEM_DB = 'https://wotlk.novawow.com' interface DeletedItem { recoverId: number itemId: number name: string count: number } function itemsError(error?: string, minutes?: number): string { switch (error) { case 'characterNotOwned': return 'El personaje seleccionado no es válido.' case 'cooldown': return `Solo puedes consultar los ítems de este personaje cada 8 horas (faltan ${minutes ?? 0} min).` case 'soapError': return 'No se pudo conectar con el servidor de juego. Inténtalo más tarde.' case 'insufficientPoints': return 'No tienes suficientes PD (se requieren 100 por ítem).' case 'captcha': return 'Verifica que no eres un robot.' case 'notAuthenticated': return 'Tu sesión ha expirado. Inicia sesión de nuevo.' default: return 'Algo ha salido mal. Inténtalo de nuevo.' } } export function RestoreItemsForm({ characters, price }: { characters: CharOption[]; price: number }) { const [character, setCharacter] = useState('') const [captcha, setCaptcha] = useState('') const [captchaKey, setCaptchaKey] = useState(0) const [busy, setBusy] = useState(false) const [items, setItems] = useState(null) const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null) async function search(e: React.FormEvent) { e.preventDefault() if (busy || !character) return setBusy(true) setMessage(null) try { const res = await fetch('/api/character/restore-items/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ character, turnstile: captcha }), }) const data: { success?: boolean; error?: string; minutes?: number; items?: DeletedItem[] } = await res.json() if (data.success) { setItems(data.items ?? []) if ((data.items ?? []).length === 0) setMessage({ ok: true, text: 'Este personaje no tiene ítems borrados recuperables.' }) } else { setMessage({ ok: false, text: itemsError(data.error, data.minutes) }) } } catch { setMessage({ ok: false, text: itemsError() }) } finally { setBusy(false) setCaptcha('') setCaptchaKey((k) => k + 1) } } async function restore(item: DeletedItem) { if (busy) return if (!window.confirm(`¿Recuperar "${item.name}" por ${price} € (SumUp)?`)) return setBusy(true) setMessage(null) try { const res = await fetch('/api/character/restore-item/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ character, recover_id: String(item.recoverId), provider: 'sumup' }), }) const data: { success?: boolean; url?: string; error?: string; message?: string } = await res.json() if (data.success && data.url) { window.location.assign(data.url) // checkout de SumUp return } setMessage({ ok: false, text: data.message || itemsError(data.error) }) setBusy(false) } catch { setMessage({ ok: false, text: itemsError() }) setBusy(false) } } function reset() { setItems(null) setMessage(null) setCharacter('') setCaptcha('') setCaptchaKey((k) => k + 1) } return (
{items === null ? (

) : (

Ítems borrados de {character}

{items.length === 0 ? (

No hay ítems borrados recuperables.

) : ( {items.map((it) => ( ))}
{it.name} {it.count > 1 ? ` x${it.count}` : ''}
)}

{ e.preventDefault(); reset() }}>Consultar otro personaje

)}
{message && {message.text}}
) }