import type { Questionnaire, QuestionnaireItem } from 'fhir/r4'; export const QUESTIONNAIRE_HIDDEN_URL = 'http://hl7.org/fhir/StructureDefinition/questionnaire-hidden'; // A scored LOINC instrument ends in an item like `{ text: "Total score", type: "decimal" }` with nothing that // says it is calculated. Akashic does not score, so a patient would meet an empty box meant to be left blank. // Never show a number we did not compute: it is hidden (the renderer honours // questionnaire-hidden, and the builder shows it) and read-only, until scoring exists. const isUncomputedTotal = (item: QuestionnaireItem): boolean => (item.type === 'decimal' || item.type === 'integer') && !item.answerOption?.length && /\btotal\s+score\b/i.test(item.text ?? ''); const markItems = (items: QuestionnaireItem[] | undefined): QuestionnaireItem[] | undefined => items?.map((item) => { const children = markItems(item.item); if (!isUncomputedTotal(item)) return children ? { ...item, item: children } : item; const others = (item.extension ?? []).filter((extension) => extension.url !== QUESTIONNAIRE_HIDDEN_URL); return { ...item, readOnly: true, extension: [...others, { url: QUESTIONNAIRE_HIDDEN_URL, valueBoolean: true }], }; }); export const hideUncomputedTotals = (questionnaire: Questionnaire): Questionnaire => ({ ...questionnaire, ...(questionnaire.item ? { item: markItems(questionnaire.item) } : {}), });