import type { HumanName } from "fhir/r4"; /** The first given name to greet by: the `usual` (known-as) name first, then official, then any. */ export const welcomeFirstName = (value: HumanName | HumanName[] | undefined | null): string | undefined => { const names = (Array.isArray(value) ? value : value ? [value] : []).filter(Boolean); const chosen = names.find((n) => n.use === "usual") ?? names.find((n) => n.use === "official") ?? names[0]; if (!chosen) return undefined; const given = chosen.given?.map((g) => g?.trim()).find(Boolean); if (given) return given; return chosen.text?.trim().split(/\s+/)[0] || undefined; }; /** Plain "Welcome" while the name is unknown, never "Welcome, undefined". */ export const welcomeTitle = (firstName: string | undefined): string => firstName ? `Welcome, ${firstName}` : "Welcome";