import type { PractitionerWithRoles } from "../../services/PractitionerService"; import type { OwnerCandidate } from "./OrganizationOwnerNotice"; // Active staff who can be named as the practice's owner. const staffCandidatesOf = (practitioners: PractitionerWithRoles[]): OwnerCandidate[] => practitioners .filter(({ practitioner }) => practitioner.active !== false && practitioner.id) .map(({ practitioner }) => { const name = practitioner.name?.[0]; return { id: practitioner.id as string, name: name?.text ?? [name?.given?.join(" "), name?.family].filter(Boolean).join(" ") ?? (practitioner.id as string), }; }) .filter((c) => c.name.trim() !== ""); // The signed-in person can always name themselves, including when the staff list cannot be read, as at // an agency-founded practice before it has an owner. export const ownerCandidatesOf = (practitioners: PractitionerWithRoles[], self?: OwnerCandidate): OwnerCandidate[] => { const staff = staffCandidatesOf(practitioners); return self && self.name.trim() !== "" && !staff.some((c) => c.id === self.id) ? [self, ...staff] : staff; };