import { LoteRiesgoCacu, RiesgoPacienteCacu } from './paciente-riesgo.models'; const CS = 'https://isssteleon.org/fhir/CodeSystem'; export const INTERVALO = 'https://isssteleon.org/fhir/StructureDefinition/intervalo-confianza'; export const ESTIMANDO = 'https://isssteleon.org/fhir/StructureDefinition/estimando'; /** A FHIR prediction: probability `p` from `inicio` to `fin`, with its interval when given. */ export function prediccion(p: number, inicio: string, fin: string, ic?: [number, number]): any { return { outcome: { coding: [{ system: `${CS}/desenlace`, code: 'cin2_mas', display: 'CIN2+ por biopsia' }] }, probabilityDecimal: p, whenPeriod: { start: inicio, end: fin }, ...(ic ? { extension: [{ url: INTERVALO, valueRange: { low: { value: ic[0] }, high: { value: ic[1] } } }] } : {}), }; } /** TEST ONLY. The external contract example: a `searchset` Bundle with patient 42's current `RiskAssessment`, as * `GET /cacu/fhir/RiskAssessment?subject=Patient/42` answers it; never belongs to the selected patient. */ export function riesgoCacuEjemplo(): any { return { resourceType: 'Bundle', type: 'searchset', total: 1, entry: [{ fullUrl: 'http://localhost/cacu/fhir/RiskAssessment/12-42', resource: { resourceType: 'RiskAssessment', id: '12-42', status: 'final', meta: { tag: [{ system: `${CS}/etiqueta`, code: 'simulado', display: 'Datos simulados' }] }, subject: { reference: 'Patient/42' }, occurrenceDateTime: '2026-10-01T00:00:00Z', method: { coding: [{ system: `${CS}/modelo`, code: 'modelo-cacu', version: '2.1.0' }] }, prediction: [ prediccion(0.08, '2026-06-01', '2027-06-01', [0.05, 0.12]), prediccion(0.21, '2026-06-01', '2029-06-01', [0.15, 0.28]), prediccion(0.34, '2026-06-01', '2031-06-01', [0.26, 0.44]), ], }, search: { mode: 'match' }, }], }; } /** TEST ONLY. What the record shows from `riesgoCacuEjemplo()`. */ export function riesgoCacuVista(): RiesgoPacienteCacu { const riesgo: LoteRiesgoCacu = { id: '12-42', modelo_nombre: 'modelo-cacu', modelo_version: '2.1.0', fecha_indice: '2026-06-01', evaluado_en: '2026-10-01T00:00:00Z', estado_lote: 'final', desenlace: 'cin2_mas', desenlace_texto: 'CIN2+ por biopsia', horizontes: [ { horizonte_anios: 1, probabilidad: 0.08, ic_bajo: 0.05, ic_alto: 0.12 }, { horizonte_anios: 3, probabilidad: 0.21, ic_bajo: 0.15, ic_alto: 0.28 }, { horizonte_anios: 5, probabilidad: 0.34, ic_bajo: 0.26, ic_alto: 0.44 }, ], estimando: null, resultados_indice: null, sin_estimacion: null, }; return { simulado: true, riesgo, riesgos: [riesgo] }; } /** TEST ONLY. `riesgoCacuEjemplo()` from a model whose scenario says what its number is (extension `estimando`). */ export function riesgoCacuConEstimando(): any { const data = riesgoCacuEjemplo(); const resource = data.entry[0].resource; resource.method.text = 'Riesgo acumulado'; resource.extension = [{ url: ESTIMANDO, extension: [ { url: 'quantity', valueString: 'Riesgo acumulado' }, { url: 'description', valueString: 'Probabilidad de CIN2+ antes de cada horizonte.' }, { url: 'outcome', valueString: 'CIN2+: biopsia LIEAG, CIS o carcinoma' }, { url: 'horizons_unit', valueString: 'años' }, { url: 'interval', valueString: 'IC 95 % por método delta' }, { url: 'otra', valueString: 'se ignora' }, ] }]; return data; } export const RESULTADOS_INDICE = 'https://isssteleon.org/fhir/StructureDefinition/resultados-indice'; /** TEST ONLY. `riesgoCacuConEstimando()` plus a cancer `RiskAssessment` of the same visit, both with the index * cytology's results: the Bundle a patient scored by the CIN2+ and the cancer models gets. Cancer comes first, as * the newer batch would. */ export function riesgoCacuDosDesenlaces(): any { const data = riesgoCacuConEstimando(); const cin2 = data.entry[0].resource; const resultados = { url: RESULTADOS_INDICE, extension: [ { url: 'escamosas', valueString: 'LIEBG' }, { url: 'vph', valueString: 'POSITIVO' }] }; cin2.extension.push(resultados); const cancer = structuredClone(cin2); cancer.id = '13-42'; cancer.method.coding[0].code = 'modelo-cacu-cancer'; cancer.extension[0].extension = cancer.extension[0].extension.map((e: any) => e.url === 'outcome' ? { ...e, valueString: 'Cáncer: biopsia de carcinoma' } : e); cancer.prediction = [ prediccion(0.004, '2026-06-01', '2027-06-01', [0.002, 0.008]), prediccion(0.009, '2026-06-01', '2029-06-01', [0.005, 0.016]), prediccion(0.015, '2026-06-01', '2031-06-01', [0.008, 0.028]), ].map(p => ({ ...p, outcome: { coding: [{ system: 'https://isssteleon.org/fhir/CodeSystem/desenlace', code: 'cancer', display: 'Cáncer cervicouterino por biopsia' }] } })); data.entry.unshift({ fullUrl: 'http://localhost/cacu/fhir/RiskAssessment/13-42', resource: cancer }); data.total = 2; return data; } export const SIN_ESTIMACION = 'https://isssteleon.org/fhir/StructureDefinition/sin-estimacion'; /** TEST ONLY. `riesgoCacuConEstimando()` plus a current cancer model that didn't score her: a `RiskAssessment` with * no prediction, marked `sin-estimacion`. */ export function riesgoCacuCancerSinEstimar(): any { const data = riesgoCacuConEstimando(); const cancer = structuredClone(data.entry[0].resource); cancer.id = '13-42'; delete cancer.prediction; cancer.extension = [{ url: SIN_ESTIMACION, extension: [{ url: 'desenlace', valueCode: 'cancer' }, { url: 'motivo', valueString: 'queda fuera de su población' }] }]; data.entry.push({ fullUrl: 'http://localhost/cacu/fhir/RiskAssessment/13-42', resource: cancer }); data.total = 2; return data; }