import { Encounter, Reference } from 'fhir/r4'; /** * The visit a clinical record should be filed against when it is being recorded from inside one. * * Three rules, and the second and third are the ones worth having a function for: * * inside a visit a reference to THAT encounter, so the form shows what the write already does * outside any undefined - there is no visit to infer, and a default would be a guess * an encounter undefined - a reference built from a missing id is `Encounter/undefined`, * with no id which is a broken pointer that looks like a chosen one * * `toReference` is passed in rather than imported because building a FHIR reference needs the service * context, and a rule this small should be testable without one. */ export const activeEncounterDefault = ( encounter: Encounter | undefined, toReference: (id: string) => Reference, ): Reference | undefined => (encounter?.id ? toReference(encounter.id) : undefined);