'use client' import { useMemo, useState } from 'react' import { DataPoint } from '@/lib/sdk/einwilligungen/types' import { validateDocument, ValidationWarning, } from '@/lib/sdk/document-generator/datapoint-helpers' interface DocumentValidationProps { dataPoints: DataPoint[] documentContent: string language?: 'de' | 'en' onInsertPlaceholder?: (placeholder: string) => void } /** * Placeholder-Vorschlag aus der Warnung extrahieren */ function extractPlaceholderSuggestion(warning: ValidationWarning): string | null { const match = warning.suggestion.match(/\[([A-Z_]+)\]/) return match ? match[0] : null } /** * DocumentValidation Komponente */ export function DocumentValidation({ dataPoints, documentContent, language = 'de', onInsertPlaceholder, }: DocumentValidationProps) { const [expandedWarnings, setExpandedWarnings] = useState([]) // Führe Validierung durch const warnings = useMemo(() => { if (dataPoints.length === 0 || !documentContent) { return [] } return validateDocument(dataPoints, documentContent, language) }, [dataPoints, documentContent, language]) // Gruppiere nach Typ const errorCount = warnings.filter(w => w.type === 'error').length const warningCount = warnings.filter(w => w.type === 'warning').length const infoCount = warnings.filter(w => w.type === 'info').length const toggleWarning = (code: string) => { setExpandedWarnings(prev => prev.includes(code) ? prev.filter(c => c !== code) : [...prev, code] ) } if (warnings.length === 0) { // Keine Warnungen - zeige Erfolgsmeldung wenn Datenpunkte vorhanden if (dataPoints.length > 0 && documentContent.length > 100) { return (

{language === 'de' ? 'Dokument valide' : 'Document valid'}

{language === 'de' ? 'Alle notwendigen Abschnitte für die ausgewählten Datenpunkte sind vorhanden.' : 'All necessary sections for the selected data points are present.'}

) } return null } return (
{/* Zusammenfassung */}
{language === 'de' ? 'Validierung:' : 'Validation:'} {errorCount > 0 && ( {errorCount} {language === 'de' ? 'Fehler' : 'Error'}{errorCount > 1 && 's'} )} {warningCount > 0 && ( {warningCount} {language === 'de' ? 'Warnung' : 'Warning'}{warningCount > 1 && (language === 'de' ? 'en' : 's')} )} {infoCount > 0 && ( {infoCount} {language === 'de' ? 'Hinweis' : 'Info'}{infoCount > 1 && (language === 'de' ? 'e' : 's')} )}
{/* Warnungen */} {warnings.map((warning, index) => { const placeholder = extractPlaceholderSuggestion(warning) const isExpanded = expandedWarnings.includes(warning.code) const isError = warning.type === 'error' return (
{/* Icon */} {isError ? ( ) : ( )}
{/* Message */}

{warning.message}

{/* Suggestion */}
{warning.suggestion}
{/* Quick-Fix Button */} {placeholder && onInsertPlaceholder && ( )} {/* Betroffene Datenpunkte */} {warning.affectedDataPoints && warning.affectedDataPoints.length > 0 && (
{isExpanded && (
    {warning.affectedDataPoints.slice(0, 5).map(dp => (
  • {language === 'de' ? dp.name.de : dp.name.en}
  • ))} {warning.affectedDataPoints.length > 5 && (
  • ... {language === 'de' ? 'und' : 'and'}{' '} {warning.affectedDataPoints.length - 5}{' '} {language === 'de' ? 'weitere' : 'more'}
  • )}
)}
)}
) })}
) } export default DocumentValidation