import { WITHHELD_COLLEAGUE_NAME } from '../staff/withheldColleague'; /** How the read of the counterparty's own record went. */ export type NameRead = | { status: 'pending' } | { status: 'loaded'; name: string } | { status: 'failed' } | { status: 'refused' }; /** What an inbox row draws where the name goes: a name, a placeholder while it loads, or a sentence saying why not. */ export type RowName = | { kind: 'name'; text: string } | { kind: 'pending' } | { kind: 'unavailable'; text: string }; export const NAME_NOT_RECORDED = 'Name not recorded'; export const NAME_WITHHELD = WITHHELD_COLLEAGUE_NAME; export const PATIENT_NAME_UNREAD = 'A patient whose name could not be read just now'; export const COLLEAGUE_NAME_UNREAD = 'A colleague whose name could not be read just now'; // While a name read is still under way, on a surface that cannot show a placeholder instead. export const NAME_LOADING = 'Name loading'; const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; // A display that is only a resource id or reference was stamped in place of a name, so it is not one. export const isIdentifierDisplay = (display: string, id?: string): boolean => display === id || UUID.test(display) || /^[A-Za-z]+\/[\w-]+$/.test(display); export const counterpartyRowName = ({ display, id, resourceType, read, }: { display?: string; id?: string; resourceType?: string; read?: NameRead; }): RowName => { const shown = display?.trim(); if (shown && !isIdentifierDisplay(shown, id)) return { kind: 'name', text: shown }; if (!id) return { kind: 'unavailable', text: 'Unknown' }; switch (read?.status) { case 'loaded': return read.name ? { kind: 'name', text: read.name } : { kind: 'unavailable', text: NAME_NOT_RECORDED }; case 'refused': return { kind: 'unavailable', text: NAME_WITHHELD }; case 'failed': return { kind: 'unavailable', text: resourceType === 'Practitioner' ? COLLEAGUE_NAME_UNREAD : PATIENT_NAME_UNREAD }; default: return { kind: 'pending' }; } }; /** * The counterparty whose stored display a row's preview must not print: the row's own reference whenever the row * itself does not name them (withheld, unreadable or still loading). A named row returns nothing, so its preview * reads as before. */ export const unnamedAuthorOf = (name: RowName, reference?: string): string | undefined => name.kind === 'name' ? undefined : reference; /** The words a row can be searched and labelled by; nothing while the name is still loading. */ export const rowNameText = (name: RowName): string => (name.kind === 'pending' ? '' : name.text);