'use client'; import { format } from 'date-fns'; import { ru } from 'date-fns/locale'; import { buildDocumentNumber, DOCUMENT_TYPES, getDocumentType, LICENSE_CATEGORIES, parseMetadata, type DocumentTypeCode, type DocumentTypeDef } from '@/lib/document-catalog'; import { UserDocument } from '@/lib/api'; import { cn } from '@/lib/utils'; function formatDateValue(value?: string) { if (!value) return ''; const date = new Date(value); if (Number.isNaN(date.getTime())) return value; return format(date, 'd MMMM yyyy', { locale: ru }); } function formatFieldValue(fieldKind: string, value: string) { if (!value) return '—'; if (fieldKind === 'date') return formatDateValue(value); if (fieldKind === 'gender') return value === 'male' ? 'Мужской' : value === 'female' ? 'Женский' : value; if (fieldKind === 'checkbox') return value === 'true' ? 'Да' : '—'; if (fieldKind === 'categories') { return value .split(',') .map((item) => item.trim()) .filter(Boolean) .join(' · '); } return value; } function fullName(values: Record) { const parts = [values.lastName, values.firstName, values.middleName].filter(Boolean); return parts.length ? parts.join(' ') : '—'; } function latinFullName(values: Record) { const parts = [values.lastNameLatin, values.firstNameLatin, values.middleNameLatin].filter(Boolean); return parts.length ? parts.join(' ') : ''; } function DocumentField({ label, value, className }: { label: string; value: string; className?: string }) { if (!value || value === '—') return null; return (

{label}

{value}

); } function PassportRfCard({ config, values, document }: { config: DocumentTypeDef; values: Record; document: UserDocument }) { return (

Российская Федерация

{config.label}

РФ

Серия и номер

{document.number || buildDocumentNumber(config, values)}

); } function ForeignPassportCard({ config, values, document }: { config: DocumentTypeDef; values: Record; document: UserDocument }) { const latin = latinFullName(values); return (

Passport

{config.label}

P

No.

{document.number || values.number || '—'}

{latin ? : null}
); } function DriverLicenseCard({ config, values, document }: { config: DocumentTypeDef; values: Record; document: UserDocument }) { const categories = formatFieldValue('categories', values.categories ?? ''); return (

Водительское удостоверение

{config.label}

RU

Номер

{document.number || values.number || '—'}

{values.specialMarks ? : null}
); } function VehicleRegistrationCard({ config, values, document }: { config: DocumentTypeDef; values: Record; document: UserDocument }) { return (

Свидетельство о регистрации

{config.label}

СТС

Серия и номер

{document.number || values.seriesNumber || '—'}

Госномер

{values.plateNumber || '—'}

); } function GenericDocumentCard({ config, values, document }: { config: DocumentTypeDef; values: Record; document: UserDocument }) { const Icon = config.icon; const visibleFields = config.fields .map((field) => ({ label: field.label, value: formatFieldValue(field.kind, values[field.key] ?? (field.key === 'issuedAt' ? document.issuedAt?.slice(0, 10) ?? '' : field.key === 'expiresAt' ? document.expiresAt?.slice(0, 10) ?? '' : '')) })) .filter((field) => field.value && field.value !== '—'); return (

Документ

{config.label}

{document.number || buildDocumentNumber(config, values)}

{visibleFields.map((field) => ( ))}
); } export function DocumentViewCard({ documentType, document }: { documentType: DocumentTypeCode; document: UserDocument; }) { const config = getDocumentType(documentType) ?? DOCUMENT_TYPES.find((item) => item.type === documentType); if (!config) return null; const metadata = parseMetadata(document.metadataJson); const values: Record = {}; for (const field of config.fields) { const raw = metadata[field.key]; values[field.key] = typeof raw === 'string' ? raw : ''; if (field.latinKey) { const latinRaw = metadata[field.latinKey]; values[field.latinKey] = typeof latinRaw === 'string' ? latinRaw : ''; } } if (document.issuedAt) values.issuedAt = document.issuedAt.slice(0, 10); if (document.expiresAt) values.expiresAt = document.expiresAt.slice(0, 10); switch (documentType) { case 'PASSPORT_RF': return ; case 'FOREIGN_PASSPORT': return ; case 'DRIVER_LICENSE': return ; case 'VEHICLE_REGISTRATION': return ; default: return ; } } export function DriverLicenseCategoryBadges({ value }: { value: string }) { const selected = new Set(value.split(',').map((item) => item.trim()).filter(Boolean)); if (!selected.size) return null; return (
{LICENSE_CATEGORIES.filter((category) => selected.has(category)).map((category) => ( {category} ))}
); }