import type { Encounter } from 'fhir/r4'; const CLOSED: ReadonlySet = new Set(['finished', 'cancelled', 'entered-in-error']); /** * The patient a booking may be seeded with: the open visit's own `Encounter.subject`, and nobody else. * No open visit, a closed one, or a subject that is not a Patient gives nothing - a wrong guess is a * booking on the wrong record. */ export const openVisitPatientId = (encounter: Encounter | undefined): string | undefined => { if (!encounter || CLOSED.has(encounter.status)) return undefined; const reference = encounter.subject?.reference; return reference?.startsWith('Patient/') ? reference.slice('Patient/'.length) || undefined : undefined; };