import type { Coding, Extension } from "fhir/r4"; // Enrolment is three-state: absent `isEnrolled` is Not given, never No. It lives inside a tribe's extension. export const TRIBAL_AFFILIATION_URL = "http://hl7.org/fhir/us/core/StructureDefinition/us-core-tribal-affiliation"; /** The stored answer: true, false, or undefined when nobody answered. */ export const enrolledOf = (tribal: Extension | undefined): boolean | undefined => tribal?.extension?.find((child) => child.url === "isEnrolled")?.valueBoolean; /** The extension for one tribe, with `isEnrolled` only when the answer is Yes or No. */ export const tribalAffiliationExtension = (tribe: Coding, enrolled: boolean | undefined): Extension => ({ url: TRIBAL_AFFILIATION_URL, extension: [ { url: "tribalAffiliation", valueCodeableConcept: { coding: [tribe], text: tribe.display } }, ...(typeof enrolled === "boolean" ? [{ url: "isEnrolled", valueBoolean: enrolled }] : []), ], }); export type EnrolledChoice = "not-given" | "yes" | "no"; /** The three answers a form offers, in the deck's order. */ export const ENROLLED_OPTIONS: { value: EnrolledChoice; label: string }[] = [ { value: "not-given", label: "Not given" }, { value: "yes", label: "Yes" }, { value: "no", label: "No" }, ]; export const enrolledChoice = (enrolled: boolean | undefined): EnrolledChoice => enrolled === undefined ? "not-given" : enrolled ? "yes" : "no"; export const enrolledFromChoice = (choice: string): boolean | undefined => choice === "yes" ? true : choice === "no" ? false : undefined; /** What a read view shows. No tribe means the question cannot have been answered. */ export const enrolledLabel = (hasTribe: boolean, enrolled: boolean | undefined): string => hasTribe && enrolled !== undefined ? (enrolled ? "Yes" : "No") : "";