/** * Source bridge — converts JSON-LD source entries to NonVerbalSource. * * Shared by Figure, Table, Formula bridges (all three use the same source * shape, mirroring glossarist's ConceptSource). One function, one shape. */ import type { NonVerbalSource, NonVerbalSourceOrigin, NonVerbalSourceRef, NonVerbalSourceLocality } from './types'; import { pickField } from './prefix'; function refFromJsonLd(raw: Record | string | undefined): NonVerbalSourceRef | undefined { if (!raw) return undefined; if (typeof raw === 'string') return { source: raw }; const r: NonVerbalSourceRef = {}; const source = pickField(raw, 'source'); const id = pickField(raw, 'id'); const version = pickField(raw, 'version'); const text = pickField(raw, 'text'); if (source) r.source = source; if (id) r.id = id; if (version) r.version = version; if (text) r.text = text; return Object.keys(r).length ? r : undefined; } function localityFromJsonLd(raw: Record | undefined): NonVerbalSourceLocality | undefined { if (!raw) return undefined; const out: NonVerbalSourceLocality = {}; const t = pickField(raw, 'localityType') ?? (raw.type as string | undefined); const rf = pickField(raw, 'referenceFrom') ?? (raw.reference_from as string | undefined); const rt = pickField(raw, 'referenceTo') ?? (raw.reference_to as string | undefined); if (t) out.type = t; if (rf) out.referenceFrom = rf; if (rt) out.referenceTo = rt; return Object.keys(out).length ? out : undefined; } function originFromJsonLd(raw: Record | undefined): NonVerbalSourceOrigin | undefined { if (!raw) return undefined; const out: NonVerbalSourceOrigin = {}; const ref = refFromJsonLd(pickField>(raw, 'ref')); const locality = localityFromJsonLd(pickField>(raw, 'locality')); const link = pickField(raw, 'link'); const id = pickField(raw, 'id'); const version = pickField(raw, 'version'); const source = pickField(raw, 'source'); if (ref) out.ref = ref; if (locality) out.locality = locality; if (link) out.link = link; if (id) out.id = id; if (version) out.version = version; if (source) out.source = source; return Object.keys(out).length ? out : undefined; } export function sourceFromJsonLd(raw: Record | undefined): NonVerbalSource | undefined { if (!raw) return undefined; const out: NonVerbalSource = {}; const id = pickField(raw, 'id'); const type = pickField(raw, 'sourceType') ?? pickField(raw, 'type'); const status = pickField(raw, 'sourceStatus') ?? pickField(raw, 'status'); const modification = pickField(raw, 'modification'); const origin = originFromJsonLd(pickField>(raw, 'origin')); if (id) out.id = id; if (type) out.type = type; if (status) out.status = status; if (modification) out.modification = modification; if (origin) out.origin = origin; return Object.keys(out).length ? out : undefined; } export function sourcesFromJsonLd(raw: unknown): NonVerbalSource[] { if (!Array.isArray(raw)) return []; const out: NonVerbalSource[] = []; for (const entry of raw) { if (!entry || typeof entry !== 'object') continue; const s = sourceFromJsonLd(entry as Record); if (s) out.push(s); } return out; }