import type { HealthcareService, Location } from 'fhir/r4'; /** * WHERE A SERVICE IS OFFERED, in the words a front desk uses. * * It exists to separate two catalogue rows that read the same. MEASURED at Great River: the visit-service * picker lists **Consultation twice**, and the two are not equivalent - one is scoped to West Burlington * Hospital and Southeast Iowa Regional Medical Center, the other is scoped to nothing at all, and * choosing the second is answered with *"Nobody at this practice currently offers Consultation"*. Same * name, same type coding; the location scope is the whole difference between a booking and a dead end. * * A SERVICE THAT NAMES NO LOCATION IS NOT A SERVICE OFFERED EVERYWHERE, and saying so would be the more * useful-sounding lie. It is a service nobody has tied to a place, which is exactly the state the * unbookable row is in - so the phrase points at the real condition rather than promising availability * this list cannot check. * * RETURNS UNDEFINED WHEN THE NAMES CANNOT BE RESOLVED, never a reference or an id. A row reading * "Consultation - Location/cfd68bc5" is worse than an undistinguished one: it is noise that looks like * information, and `distinguishDuplicateOptions` treats undefined as "I have no fact to offer" and * leaves the plain label alone. */ export const NO_LOCATION_SUMMARY = 'Not tied to a location'; export const serviceLocationSummary = ( service: HealthcareService | undefined, locations: Location[] | undefined, ): string | undefined => { const references = service?.location ?? []; if (references.length === 0) return NO_LOCATION_SUMMARY; const names = references .map((reference) => reference.reference?.split('/').pop()) .map((id) => (id ? locations?.find((location) => location.id === id)?.name?.trim() : undefined)) .filter((name): name is string => Boolean(name)); return names.length > 0 ? names.join(', ') : undefined; };