import type { EligibilityState } from './eligibility'; /** The `$eligibility` scenarios that stand for each follow-up status, the first that has a * visit speaking for her. A case has no disease-risk estimate to be eligible for. */ const SCENARIOS: Record = { pre_colposcopy: ['pre_colposcopy', 'surveillance'], post_colposcopy: ['post_colposcopy'], }; /** What the risk section says when she is not eligible in her clinical scenario, from its latest * visit indexed by `day`; nothing when she is eligible or the backend has no visit of it. */ export function ineligibility( state: EligibilityState, status: string, day: string, ): string | undefined { // a treated patient has no risk estimate, whatever visit was indexed if (status === 'post_treatment') return 'No disponible por: paciente ya tratada.'; if (state.kind !== 'evaluated') return undefined; for (const scenario of SCENARIOS[status] ?? []) { // visits come latest first const latest = state.groups .find((g) => g.code === scenario) ?.visits.find((v) => v.indexDate <= day); if (latest) { return latest.eligible ? undefined : `No disponible por: ${latest.reasons.map((r) => r.text).join('; ')}.`; } } return undefined; } /** Which cytology her estimates speak of, against her latest by the cut-off. After the first * post-colposcopy visit nothing is evaluated; otherwise a later cytology is only said. */ export function currency( estimates: { model: string; index: string }[], cytologies: string[], ): { sample?: string; excluded?: string; note?: string } { const sample = estimates.map((e) => e.index.slice(0, 10)).sort().at(-1); if (!sample) return {}; const latest = cytologies.map((d) => d.slice(0, 10)).sort().at(-1); if (!latest || latest <= sample) { return { sample, note: `Estimación de la citología del ${sample}.` }; } if (estimates.some((e) => e.model.includes('post_colposcopy'))) { return { sample, excluded: `No disponible por: hay una citología posterior a la primera visita poscolposcopía (${latest}) y solo se evalúa esa primera visita.`, }; } return { sample, note: `Estimación de la citología del ${sample}. Hay una citología posterior, del ${latest}, que no se evalúa.`, }; }