import type { ContactPoint, HumanName } from "fhir/r4"; import { formatFhirHumanName } from "./fhirHumanName.tsx"; export type RelatedPersonOptionView = { label: string; /** Unidentified people cannot be selected for record access. */ selectable: boolean; }; /** Builds a label and access-safe selection state for a related person. */ export const relatedPersonOption = (input: { name?: HumanName[]; contact?: ContactPoint[]; relationshipText?: string; loading?: boolean; }): RelatedPersonOptionView => { const relationship = input.relationshipText ? input.relationshipText.charAt(0).toUpperCase() + input.relationshipText.slice(1) : ""; const withRelationship = (value: string) => (relationship ? `${value} (${relationship})` : value); const projected = formatFhirHumanName(input.name, { emptyText: "" }); if (projected) return { label: withRelationship(projected), selectable: true }; // Contact identifies an invitation when its projected name is unavailable. const contact = (input.contact ?? []).map((c) => c.value).find((v) => !!v && v.trim() !== ""); if (contact) return { label: withRelationship(contact), selectable: true }; if (input.loading) return { label: "Loading…", selectable: false }; return { label: "Name unavailable", selectable: false }; };