export type InvitationRecord = {
  id: bigint;
  portfolioId: bigint;
  role: "creator" | "admin" | "read" | "write";
};

export type MembershipPayload = {
  portfolioId: bigint;
  userId: bigint;
  role: "creator" | "admin" | "read" | "write";
};

export type InvitationApplicationPlan = {
  memberships: MembershipPayload[];
  invitationsToDelete: bigint[];
};

export function planInvitationApplication({
  userId,
  invitations,
  existingPortfolioIds,
}: {
  userId: bigint;
  invitations: InvitationRecord[];
  existingPortfolioIds: Set<bigint>;
}): InvitationApplicationPlan {
  const memberships: MembershipPayload[] = [];
  const invitationsToDelete: bigint[] = [];

  for (const inv of invitations) {
    invitationsToDelete.push(inv.id);
    if (!existingPortfolioIds.has(inv.portfolioId)) {
      memberships.push({
        portfolioId: inv.portfolioId,
        userId,
        role: inv.role,
      });
    }
  }

  return { memberships, invitationsToDelete };
}
