import { useState } from 'react'; import { ClipboardPaste, CheckCircle, AlertTriangle } from 'lucide-react'; import { toast } from 'sonner'; import api, { type ImportResult } from '@/lib/api'; import { formatAmount } from '@/lib/format'; import { formatShortDate } from '@/lib/dates'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; interface Props { onClose: () => void; onImported: () => void; } export default function PasteImportModal({ onClose, onImported }: Props) { const [text, setText] = useState(''); const [bank, setBank] = useState('BAC'); const [source, setSource] = useState('CREDIT_CARD'); const [importing, setImporting] = useState(false); const [result, setResult] = useState(null); const handleImport = async () => { if (!text.trim()) return; setImporting(true); try { const { data } = await api.post('/import/paste', { text, bank, source }); setResult(data); if (data.imported > 0) onImported(); } catch { toast.error('No se pudo importar - revisa el formato'); } finally { setImporting(false); } }; // Re-import ONLY the skipped lines with the dedup check disabled, so rows // that imported on the first pass are never duplicated. const handleImportDuplicates = async () => { if (!result || result.skipped.length === 0) return; const lines = text.trim().split('\n'); const dupText = result.skipped .map((s) => lines[s.line - 1]) .filter(Boolean) .join('\n'); setImporting(true); try { const { data } = await api.post('/import/paste', { text: dupText, bank, source, allow_duplicates: true, }); toast.success(`${data.imported} duplicado(s) importados`); setResult({ ...result, imported: result.imported + data.imported, skipped: [], duplicates: 0, }); onImported(); } catch { toast.error('No se pudieron importar los duplicados'); } finally { setImporting(false); } }; return ( { if (!open) onClose(); }}> Import Bank Statement {!result ? (