import type { Sheet } from '../export'; import { ANTECEDENTS } from './record-groups'; import type { Prediction, RiskScale } from './risk-scales'; import { SOURCES, Stage, STATES } from './stages'; const NONE = 'No disponible'; const percent = (p: number) => `${(100 * p).toFixed(2)} %`; const between = (p?: Prediction) => p?.low === undefined || p.high === undefined ? NONE : `${percent(p.low)} a ${percent(p.high)}`; type Facts = [string, string | undefined][]; interface Event { date: string; kind: string; text: string; } interface Test { date: string; kind: string; reported?: string; results: string[]; /** the extra findings (collapsed on the page) */ findings: string[]; } interface Form { date: string; values: Record; } /** A published estimate (risk, survival or recurrence) by horizon. */ interface Estimate { text: string; index: string; predictions: Prediction[]; } /** The curves of her clinical scenarios as the page lists them. */ export interface Analysis { title: string; scenarios: string[]; /** the titles of the figures drawn */ figures: string[]; /** her scenarios the report draws no curve for */ undrawn: string[]; } const table = (title: string, facts: Facts): Sheet => ({ title, head: ['Dato', 'Valor'], rows: facts.map(([k, v]) => [k, v || NONE]), }); const historySheet = (history: Event[]): Sheet => ({ title: 'Historial médico', head: ['Fecha', 'Tipo', 'Detalle'], rows: history.map((h) => [h.date, h.kind, h.text]), }); const testsSheet = (tests: Test[]): Sheet => ({ title: 'Pruebas y resultados', head: ['Toma', 'Estudio', 'Reporte', 'Resultados', 'Hallazgos'], rows: tests.map((t) => [ t.date, t.kind, t.reported ?? 'Pendiente', t.results.join(' · '), t.findings.join(' · '), ]), }); /** A CaCu patient's record as the tables of her PDF, in the page's order. */ export function recordSheets( facts: Facts, stages: Stage[], history: Event[], tests: Test[], scales: RiskScale[], forms: Form[], ): Sheet[] { return [ table('Datos de la paciente', facts), { title: 'Etapas del tamizaje', head: ['Etapa', 'Estado', 'Fecha', 'Procedencia'], rows: stages.map((s) => [ s.label, STATES[s.state], s.day ?? (s.requested ? `Solicitada el ${s.requested}` : ''), (s.sources ?? []).map((c) => SOURCES[c]).join(' · '), ]), }, historySheet(history), { title: 'Antecedentes y evaluación ginecológica (declarados en el formulario de la citología; no son diagnósticos confirmados)', head: ['Toma', ...ANTECEDENTS.map((a) => a.title)], rows: forms.map((f) => [ f.date, ...ANTECEDENTS.map((a) => f.values[a.field] ?? 'No documentado'), ]), }, testsSheet(tests), { title: 'Probabilidades', head: ['Estimación', 'Probabilidad', 'IC 95 %'], rows: scales.map(({ label, risk }) => [label, risk ? percent(risk.p) : NONE, between(risk)]), }, ]; } const horizon = (years: number) => years === 0 ? 'Al índice' : `${years} ${years === 1 ? 'año' : 'años'}`; /** A CaMa patient's record as the tables of her PDF: only what CaMa publishes, an absent value * said as such and no estimate made up. */ export function camaRecordSheets( facts: Facts, studies: Facts, history: Event[], tests: Test[], estimates: Estimate[], analyses: Analysis[], ): Sheet[] { const risks = estimates.flatMap((m) => m.predictions.map((p) => [m.text, m.index || NONE, horizon(p.years), percent(p.p), between(p)]), ); const curves = analyses.flatMap((a) => [ ...a.figures.map((f) => [a.title, f, 'Curva publicada']), ...a.undrawn.map((s) => [a.title, s, 'No disponible: datos insuficientes']), ...(a.figures.length || a.undrawn.length ? [] : [[a.title, a.scenarios.join('; ') || NONE, NONE]]), ]); return [ table('Datos de la paciente', facts), table('Última mastografía y resultado histopatológico', studies), historySheet(history), testsSheet(tests), { title: 'Riesgos y recomendaciones publicados', head: ['Estimación', 'Fecha índice', 'Horizonte', 'Probabilidad', 'IC 95 %'], rows: risks.length ? risks : [['Sin estimaciones publicadas', NONE, NONE, NONE, NONE]], }, ...(curves.length ? [ { title: 'Supervivencia y recurrencia por escenario clínico (estimaciones del grupo, no predicciones individuales)', head: ['Análisis', 'Escenario', 'Estado'], rows: curves, }, ] : []), ]; }