import type { HumanName } from 'fhir/r4'; import type { ActingAsTarget, IUser } from '../../services/AuthService.tsx'; import { getDisplayName } from '../../utils/fhirHumanName'; export interface AgreementSubject { subjectName: string; guardianName?: string; } // Whose agreements these are when a helper has switched into someone else's record; none for oneself. export const agreementSubject = ( actingAs: ActingAsTarget | null | undefined, user: IUser | null | undefined, ): AgreementSubject | undefined => { if (!actingAs) return undefined; const ownName = user?.fhir && 'name' in user.fhir ? getDisplayName(user.fhir.name as HumanName[] | undefined) : ''; return { subjectName: actingAs.subjectName?.trim() || 'the person you are acting for', guardianName: ownName.trim() || undefined, }; }; // The Patient these agreements are about: the one acted for, or the signed-in patient. Never a Patient merely // loaded on the page, which can still be the previous one; undefined when neither is proven. export const agreementPatientId = ( actingAs: ActingAsTarget | null | undefined, user: IUser | null | undefined, ): string | undefined => { if (actingAs) return actingAs.subjectPatientId || undefined; return user?.fhir?.resourceType === 'Patient' ? user.fhir.id || undefined : undefined; };