/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useState, useEffect } from 'react'
import { Gift, ExternalLink, Loader2, Receipt, WalletCards } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { formatNumber } from '@/lib/format'
import { cn } from '@/lib/utils'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardHeader } from '@/components/ui/card'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Skeleton } from '@/components/ui/skeleton'
import { TitledCard } from '@/components/ui/titled-card'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
import {
formatCurrency,
getDiscountLabel,
getPaymentIcon,
getMinTopupAmount,
calculatePresetPricing,
} from '../lib'
import type {
PaymentMethod,
PresetAmount,
TopupInfo,
CreemProduct,
WaffoPayMethod,
} from '../types'
import { CreemProductsSection } from './creem-products-section'
interface RechargeFormCardProps {
topupInfo: TopupInfo | null
presetAmounts: PresetAmount[]
selectedPreset: number | null
onSelectPreset: (preset: PresetAmount) => void
topupAmount: number
onTopupAmountChange: (amount: number) => void
paymentAmount: number
calculating: boolean
onPaymentMethodSelect: (method: PaymentMethod) => void
paymentLoading: string | null
redemptionCode: string
onRedemptionCodeChange: (code: string) => void
onRedeem: () => void
redeeming: boolean
topupLink?: string
loading?: boolean
priceRatio?: number
usdExchangeRate?: number
onOpenBilling?: () => void
creemProducts?: CreemProduct[]
enableCreemTopup?: boolean
onCreemProductSelect?: (product: CreemProduct) => void
enableWaffoTopup?: boolean
waffoPayMethods?: WaffoPayMethod[]
waffoMinTopup?: number
onWaffoMethodSelect?: (method: WaffoPayMethod, index: number) => void
enableWaffoPancakeTopup?: boolean
}
export function RechargeFormCard({
topupInfo,
presetAmounts,
selectedPreset,
onSelectPreset,
topupAmount,
onTopupAmountChange,
paymentAmount,
calculating,
onPaymentMethodSelect,
paymentLoading,
redemptionCode,
onRedemptionCodeChange,
onRedeem,
redeeming,
topupLink,
loading,
priceRatio = 1,
usdExchangeRate = 1,
onOpenBilling,
creemProducts,
enableCreemTopup,
onCreemProductSelect,
enableWaffoTopup,
waffoPayMethods,
waffoMinTopup,
onWaffoMethodSelect,
enableWaffoPancakeTopup,
}: RechargeFormCardProps) {
const { t } = useTranslation()
const [localAmount, setLocalAmount] = useState(topupAmount.toString())
useEffect(() => {
setLocalAmount(topupAmount.toString())
}, [topupAmount])
const handleAmountChange = (value: string) => {
setLocalAmount(value)
const numValue = parseInt(value) || 0
if (numValue >= 0) {
onTopupAmountChange(numValue)
}
}
const hasConfigurableTopup =
topupInfo?.enable_online_topup ||
topupInfo?.enable_stripe_topup ||
enableWaffoTopup ||
enableWaffoPancakeTopup
const hasAnyTopup = hasConfigurableTopup || enableCreemTopup
const hasStandardPaymentMethods =
Array.isArray(topupInfo?.pay_methods) && topupInfo.pay_methods.length > 0
const hasWaffoPaymentMethods =
Array.isArray(waffoPayMethods) && waffoPayMethods.length > 0
const minTopup = getMinTopupAmount(topupInfo)
if (loading) {
return (