import { useState, useMemo } from 'react' import { Plus, X } from 'lucide-react' import { useTranslation } from 'react-i18next' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { StatusBadge } from '@/components/status-badge' import { safeJsonParseWithValidation } from '../utils/json-parser' import { isArray } from '../utils/json-validators' type AmountOptionsVisualEditorProps = { value: string onChange: (value: string) => void } export function AmountOptionsVisualEditor({ value, onChange, }: AmountOptionsVisualEditorProps) { const { t } = useTranslation() const [newAmount, setNewAmount] = useState('') const amounts = useMemo(() => { const parsed = safeJsonParseWithValidation(value, { fallback: [], validator: isArray, validatorMessage: t('Amount options must be a JSON array'), context: 'amount options', }) return parsed .filter((item) => typeof item === 'number' || !isNaN(Number(item))) .map(Number) .sort((a, b) => a - b) }, [value, t]) const handleAdd = () => { const amount = parseFloat(newAmount) if (isNaN(amount) || amount <= 0) { return } try { const updatedAmounts = [...amounts, amount] .filter((v, i, a) => a.indexOf(v) === i) // Remove duplicates .sort((a, b) => a - b) onChange(JSON.stringify(updatedAmounts, null, 2)) setNewAmount('') } catch (_error) { // eslint-disable-next-line no-console console.error('Failed to add amount:', _error) } } const handleRemove = (amount: number) => { try { const updatedAmounts = amounts.filter((a) => a !== amount) onChange(JSON.stringify(updatedAmounts, null, 2)) } catch (_error) { // eslint-disable-next-line no-console console.error('Failed to remove amount:', _error) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault() handleAdd() } } return (

{t('Preset recharge amounts displayed to users')}

{amounts.length === 0 ? (
{t( 'No amount options configured. Add amounts below to get started.' )}
) : (
{amounts.map((amount) => ( ${amount} ))}
)}
setNewAmount(e.target.value)} onKeyDown={handleKeyDown} />
) }