import { CompanyIdFieldEnvelope } from '../../service/company-id/company-id.interface'; interface SurfaceEntry { platform: string; url: string; type: string; ownership: string; propagation: string; match: null; last_confirmed: string | null; } interface PersonEntry { name: string; role: string; } function splitEntries(draft: string): string[] { const byLine = draft .split('\n') .map(line => line.trim()) .filter(Boolean); if (byLine.length > 1) return byLine; return (byLine[0] || '') .split(/[,·]/) .map(part => part.trim()) .filter(Boolean); } function surfaceFromUrl(rawUrl: string): SurfaceEntry { const cleaned = rawUrl.replace(/^https?:\/\//i, '').replace(/\/$/, ''); const host = cleaned.split('/')[0] || cleaned; const label = host.replace(/^www\./, '').split('.')[0] || 'site'; const socialHosts = [ 'instagram', 'facebook', 'tiktok', 'x', 'twitter', 'youtube', 'linkedin', 'pinterest', ]; const marketplaceHosts = ['amazon', 'emag', 'etsy', 'ebay', 'allegro']; const reviewHosts = ['trustpilot', 'google', 'yelp']; let type = 'site'; if (socialHosts.includes(label)) type = 'social'; else if (marketplaceHosts.includes(label)) type = 'marketplace'; else if (reviewHosts.includes(label)) type = 'review'; return { platform: label, url: cleaned, type, ownership: 'owned', propagation: 'manual', match: null, last_confirmed: null, }; } export function parseDraft( kind: CompanyIdFieldEnvelope['kind'], draft: string ): unknown { const trimmed = draft.trim(); if (!trimmed) return null; if (kind === 'text' || kind === 'mono') return trimmed; if (kind === 'tags' || kind === 'ranked' || kind === 'lines') { return splitEntries(trimmed); } if (kind === 'kv') { return trimmed .split('\n') .map(line => line.trim()) .filter(Boolean) .map(line => { const separatorIndex = line.indexOf(':'); if (separatorIndex < 0) return [line, '']; return [ line.slice(0, separatorIndex).trim(), line.slice(separatorIndex + 1).trim(), ]; }); } if (kind === 'surfaces') { return splitEntries(trimmed).map(surfaceFromUrl); } if (kind === 'people') { return trimmed .split('\n') .map(line => line.trim()) .filter(Boolean) .map((line): PersonEntry => { const parts = line.split(/\s*[-·,]\s*/); return { name: parts[0] || line, role: parts.slice(1).join(', ') }; }); } return trimmed; } export function serializeValue( kind: CompanyIdFieldEnvelope['kind'], value: unknown ): string { if (value == null) return ''; if (typeof value === 'string') return value; if (Array.isArray(value)) { if (kind === 'kv') { return (value as [string, string][]) .map(pair => `${pair[0]}: ${pair[1]}`) .join('\n'); } if (kind === 'surfaces') { return (value as SurfaceEntry[]).map(surface => surface.url).join('\n'); } if (kind === 'people') { return (value as PersonEntry[]) .map(person => person.role ? `${person.name} - ${person.role}` : person.name ) .join('\n'); } return (value as string[]).join('\n'); } return ''; } export function previewValue(value: unknown): string { if (value == null) return ''; if (typeof value === 'string') return value; if (Array.isArray(value)) { return value .map(entry => { if (typeof entry === 'string') return entry; if (Array.isArray(entry)) return entry.join(': '); if (entry && typeof entry === 'object') { const record = entry as Record; return String(record.url ?? record.name ?? record.label ?? ''); } return String(entry); }) .filter(Boolean) .join(', '); } return String(value); }