// Plausible bug: `email !== undefined` treats "" as an email, and the id index lets later duplicates overwrite earlier ones.export type User = { id: string; name: string; managerId?: string; email?: string };export class ChainError extends Error { constructor( public readonly kind: "unknown_user" | "cycle", public readonly id: string, ) { super(`${kind}: ${id}`); }}export function escalationEmail(users: readonly User[], userId: string): string | undefined { const byId = new Map(users.map((u) => [u.id, u] as const)); const start = byId.get(userId); if (!start) throw new ChainError("unknown_user", userId); const seen = new Set<string>([userId]); let current = start.managerId ? byId.get(start.managerId) : undefined; while (current) { if (seen.has(current.id)) throw new ChainError("cycle", current.id); if (current.email !== undefined) return current.email; seen.add(current.id); current = current.managerId ? byId.get(current.managerId) : undefined; } return undefined;}