'use client' import { useState } from 'react' import { useTranslations, useLocale } from 'next-intl' import { CharacterSelect, type CharOption } from '@/components/CharacterSelect' import { WowheadLink } from '@/components/WowheadLink' import { wowheadIcon } from '@/lib/wowhead' import type { StoreCategory, StoreItem } from '@/lib/store' const ICON_FALLBACK = 'inv_misc_questionmark' /** Nodo de categoría colapsable; renderiza sus ítems solo cuando está abierto. */ function CategoryNode({ cat, depth, cart, onAdd, }: { cat: StoreCategory depth: number cart: Map onAdd: (it: StoreItem) => void }) { const t = useTranslations('Store') const [open, setOpen] = useState(false) const spanClass = depth === 0 ? 'store-cat' : depth === 1 ? 'store-subcat' : 'store-sub-subcat' return (
  • setOpen((o) => !o)}> {cat.name} {open && ( <> {cat.children.length > 0 && (
      {cat.children.map((c) => ( ))}
    )} {cat.items.length > 0 && (
    {cat.items.map((it) => { const added = cart.has(it.id) return (
    {/* eslint-disable-next-line @next/next/no-img-element */} {it.name}

    {it.name}


    {t('quantity')}: {it.qty}

    {it.currency === 'pv' ? 'PV' : 'PD'}:{' '} {it.price}


    ) })}
    )} )}
  • ) } type PayMethod = 'balance' | 'stripe' | 'sumup' export function StoreBrowser({ characters, dp, vp }: { characters: CharOption[]; dp: number; vp: number }) { const t = useTranslations('Store') const locale = useLocale() const [character, setCharacter] = useState('') const [catalog, setCatalog] = useState(null) const [loading, setLoading] = useState(false) const [cart, setCart] = useState>(new Map()) const [method, setMethod] = useState('balance') const [sending, setSending] = useState(false) const [modal, setModal] = useState<{ ok: boolean; text: string } | null>(null) async function onSelect(name: string) { setCharacter(name) setCart(new Map()) if (!name || catalog) return setLoading(true) try { const res = await fetch('/api/store/catalog', { credentials: 'same-origin' }) const data: { success?: boolean; catalog?: StoreCategory[] } = await res.json() setCatalog(data.success ? data.catalog ?? [] : []) } catch { setCatalog([]) } finally { setLoading(false) } } function addItem(it: StoreItem) { setCart((prev) => { const next = new Map(prev) next.set(it.id, it) return next }) } function removeItem(id: number) { setCart((prev) => { const next = new Map(prev) next.delete(id) return next }) } const lines = [...cart.values()] const pdTotal = lines.filter((l) => l.currency === 'pd').reduce((s, l) => s + l.price, 0) const vpTotal = lines.filter((l) => l.currency === 'pv').reduce((s, l) => s + l.price, 0) // 100 PD = 1 €, 200 PV = 1 € (igual que en lib/store storeEuroTotal). const eurTotal = Math.round((pdTotal / 100 + vpTotal / 200) * 100) / 100 async function send() { if (sending || cart.size === 0) return const card = method === 'stripe' || method === 'sumup' const ok = card ? window.confirm(t('confirmCard', { eur: eurTotal, character })) : window.confirm(t('confirm', { character, pd: pdTotal, vp: vpTotal })) if (!ok) return setSending(true) try { const res = await fetch('/api/store/send', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ character, items: [...cart.keys()], provider: method, locale }), }) const data: { success?: boolean; error?: string; url?: string; min?: number } = await res.json() if (data.success && data.url) { window.location.href = data.url // pasarela (Stripe/SumUp) return } if (data.success) { setModal({ ok: true, text: t('successMsg', { character }) }) setCart(new Map()) } else if (data.error === 'amountTooLow') { setModal({ ok: false, text: t('errors.amountTooLow', { min: data.min ?? 1 }) }) } else { setModal({ ok: false, text: t.has(`errors.${data.error}`) ? t(`errors.${data.error}`) : t('errors.generic') }) } } catch { setModal({ ok: false, text: t('errors.generic') }) } finally { setSending(false) } } return ( <>

    {t('choosePlayer')}


    PD: {dp} · PV: {vp}

    {loading &&

    {t('loading')}

    } {catalog && !loading && (
      {catalog.map((c) => ( ))}
    {/* Carrito */}
    {lines.length > 0 && (

    {t('payMethod')}

    {([ { id: 'balance' as PayMethod, label: t('payBalance'), sub: `${pdTotal} PD · ${vpTotal} PV` }, { id: 'stripe' as PayMethod, label: t('payStripe'), sub: `${eurTotal} €` }, { id: 'sumup' as PayMethod, label: t('paySumUp'), sub: `${eurTotal} €` }, ]).map((o) => ( ))}
    )} {lines.length === 0 ? ( ) : ( lines.map((l) => ( )) )}
    {t('colItem')} {t('colQty')} {t('colValue')}
    {t('cartEmpty')}
    {l.name} {l.qty} {l.currency === 'pv' ? 'PV' : 'PD'}: {l.price}

    {lines.length} PD: {pdTotal}
    PV: {vpTotal}
    )} {modal && (
    setModal(null)}>
    e.stopPropagation()}> setModal(null)}>×

    {modal.text}

    )} ) }