import type { CollaborationTeam, TeamMembership } from '../services/collaborationTeam'; /** One of the patient's collaboration teams, as seen from one practice. */ export interface OrgCollabTeam { teamId: string; name: string; /** The people from this practice who are active on the team. */ fromHere: string[]; /** Active members in all, this practice's and everyone else's. */ memberCount: number; } export interface OrgStaffMember { id: string; name?: string; } /** * The patient's own teams that have somebody from this practice on them. * * A member counts as from here when they are on the practice's staff list, or, where that list could * not be read, when the server names this practice among theirs. */ export const teamsWithPeopleFrom = ( teams: Array, staff: OrgStaffMember[] | null, organizationName?: string, ): OrgCollabTeam[] => { const staffById = new Map((staff ?? []).map((s) => [`Practitioner/${s.id}`, s])); const practice = organizationName?.trim(); return teams .filter((team) => team.status !== 'dissolved') .map((team) => { const active = (team.members ?? []).filter((m) => m.state === 'active'); const fromHere = active .filter((m) => m.memberClass === 'practitioner') .filter((m) => staffById.has(m.memberRef) || Boolean(practice && m.organizationDisplays?.includes(practice))) .map((m) => m.displayName?.trim() || staffById.get(m.memberRef)?.name || 'A practitioner'); return { teamId: team.teamId, name: team.name, fromHere, memberCount: active.length }; }) .filter((team) => team.fromHere.length > 0); }; /** "A", "A and B", "A, B and C". */ export const joinNames = (names: string[]): string => names.length <= 1 ? (names[0] ?? '') : `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`;