import type { VersionMember } from './derive';

export interface RecommendedAssigneeDraft {
  recommendedAssigneeName?: string;
  recommendedAssigneeReason?: string;
}

export interface ResolvedRecommendedAssignee {
  name: string;
  role: VersionMember['role'];
  reason?: string;
}

export function resolveRecommendedAssignee(
  draft: object,
  members: VersionMember[] | undefined,
): ResolvedRecommendedAssignee | undefined {
  const recommendation = draft as RecommendedAssigneeDraft;
  if (!recommendation.recommendedAssigneeName) return undefined;
  const member = (members ?? []).find((candidate) => candidate.name === recommendation.recommendedAssigneeName);
  if (!member) return undefined;

  const reason = recommendation.recommendedAssigneeReason?.trim();
  return {
    name: member.name,
    role: member.role,
    ...(reason ? { reason } : {}),
  };
}
