'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import { Link } from '@/i18n/navigation' const LOGOS = '/ns-themes/ns-ryu/ns-images/ns-logos' const RANKS = '/ns-themes/ns-ryu/ns-images/ns-ranks' type TabId = 'Info' | 'Stripe' | 'SumUp' // [archivo, nivel, umbral en PD, tipo de rango]. El euro = PD / 100 (1 unidad = 100 PD). const RANK_ROWS: [string, number, number, 'from' | 'upto' | 'over'][] = [ ['1_Newbie', 1, 0, 'from'], ['2_Rookie', 2, 100, 'upto'], ['3_Apprentice', 3, 200, 'upto'], ['4_Explorer', 4, 300, 'upto'], ['5_Contributor', 5, 400, 'upto'], ['6_Enthusiast', 6, 500, 'upto'], ['7_Collaborator', 7, 600, 'upto'], ['8_Regular', 8, 700, 'upto'], ['9_RisingStar', 9, 800, 'upto'], ['10_Proficient', 10, 900, 'upto'], ['11_Experienced', 11, 1000, 'upto'], ['12_Mentor', 12, 5000, 'upto'], ['13_Veteran', 13, 10000, 'upto'], ['14_GrandMaster', 14, 10000, 'over'], ] export function DPointsTabs({ realm, currency }: { realm: string; currency: string }) { const t = useTranslations('Points') const [active, setActive] = useState('Info') const [showRanks, setShowRanks] = useState(false) const sym = currency === 'usd' ? '$' : '€' return ( <>
{t('tabs.backToAccount')} {t('tabs.back')}

{active === 'Info' && (
{t('tabs.whatArePd')}

{t('tabs.whatArePdText')}




{t('tabs.availableLevels')} {!showRanks ? (

setShowRanks(true)}>{t('tabs.show')}

) : (

setShowRanks(false)}>{t('tabs.hide')}

)} {showRanks && (
{RANK_ROWS.map(([file, level, pd, type]) => ( ))}
{t('tabs.level', {t('tabs.level', { n: level })} {t(`tabs.range${type === 'from' ? 'From' : type === 'over' ? 'Over' : 'UpTo'}`, { pd: String(pd), price: String(pd / 100), sym })}
)}


{t('tabs.purchaseMethods')}

{t('tabs.purchaseMethodsText')}

- Stripe {t('tabs.stripeMethod')}

- SumUp {t('tabs.sumupMethod')}




{t('tabs.howManyPd')}

{t('tabs.howManyPdText', { sym })}




{t('tabs.doubts')}

{t('tabs.doubtsText1')}

{t('tabs.doubtsText2', { realm })}

)} {active === 'Stripe' && } {active === 'SumUp' && }
) } /* --------------------------- Lógica de pago común --------------------------- */ function checkoutErrorKey(error?: string): string { switch (error) { case 'notConfigured': return 'notConfigured' case 'invalidAmount': return 'invalidAmount' case 'notAuthenticated': return 'notAuthenticated' default: return 'generic' } } function usePayment(provider: 'stripe' | 'sumup') { const t = useTranslations('Points') const checkoutError = (error?: string) => t(`tabs.checkoutErrors.${checkoutErrorKey(error)}`) const [amount, setAmount] = useState('') const [accepted, setAccepted] = useState(false) const [busy, setBusy] = useState(false) const [err, setErr] = useState(null) async function pay(e: React.FormEvent, sym: string) { e.preventDefault() if (!accepted) { alert(t('tabs.mustAccept')) return } const n = Number(amount) if (!Number.isInteger(n) || n < 1 || n > 200) { setErr(checkoutError('invalidAmount')) return } if (!window.confirm(t('tabs.confirmPurchase', { points: n * 100, sym, amount: n }))) return setBusy(true) setErr(null) try { const res = await fetch('/api/dpoints/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ provider, amount: n }), }) const d: { success?: boolean; url?: string; error?: string } = await res.json() if (d.success && d.url) { window.location.href = d.url return } setErr(checkoutError(d.error)) setBusy(false) } catch { setErr(checkoutError()) setBusy(false) } } return { amount, setAmount, accepted, setAccepted, busy, err, pay } } /* -------------------------------- Formulario -------------------------------- */ function AmountForm({ provider, sym, buttonLabel, }: { provider: 'stripe' | 'sumup' sym: string buttonLabel: string }) { const t = useTranslations('Points') const st = usePayment(provider) const points = Number(st.amount) > 0 ? Number(st.amount) * 100 : 0 const currency = sym === '$' ? 'USD' : 'EUR' return ( <>

{t('tabs.rate', { sym })}

{t('tabs.minMax', { sym })}


{t('tabs.important')}

{t('tabs.integersOnly')}

{t('tabs.example')}


{t('tabs.amountLabel', { currency })}

st.pay(e, sym)}> st.setAmount(e.target.value)} required />{' '} {currency} {points > 0 &&

{t('tabs.willReceive', { points })}

}

st.setAccepted(e.target.checked)} required />

{st.err &&

{st.err}

}
) } /* ---------------------------------- Stripe ---------------------------------- */ function StripeTab({ realm, sym }: { realm: string; sym: string }) { const t = useTranslations('Points') return (
{t('tabs.whatIsStripe')}Stripe

{t.rich('tabs.stripeAboutText', { realm, br: () =>
, link: (c) => {c}, })}




{t('tabs.howStripe')}

{t('tabs.stripeHowText')}




{t('tabs.step1')}

{t.rich('tabs.stripeStep2', { s: (c) => {c} })}

{t('tabs.stripeStep3')}


{t('tabs.reminderMin', { sym })}

{t.rich('tabs.wrongExamples', { sym, s: (c) => {c} })}

{t.rich('tabs.rightExamples', { sym, s: (c) => {c} })}




) } /* ---------------------------------- SumUp ----------------------------------- */ function SumUpTab({ realm, sym }: { realm: string; sym: string }) { const t = useTranslations('Points') return (
{t('tabs.whatIsSumup')}SumUp

{t.rich('tabs.sumupAboutText', { realm, br: () =>
, link: (c) => {c}, })}




{t('tabs.howSumup')}

{t('tabs.sumupHowText')}




{t('tabs.step1')}

{t.rich('tabs.sumupStep2', { s: (c) => {c} })}

{t('tabs.sumupStep3')}


{t('tabs.reminderMin', { sym })}

{t.rich('tabs.wrongExamples', { sym, s: (c) => {c} })}

{t.rich('tabs.rightExamples', { sym, s: (c) => {c} })}




) }