import { useMutation, useQueryClient } from '@tanstack/react-query';

import { PullRequestsService } from '@/client/services/PullRequestsService';

export function useAssignPrLabel(
  workspaceName: string | null,
  repoName: string | null,
  number: number | null,
) {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: (labelId: string) => {
      if (!workspaceName || !repoName || number === null) throw new Error('Missing params');
      return PullRequestsService.prAssignLabel({ workspaceName, repoName, number, labelId });
    },
    onSuccess: () => {
      qc.invalidateQueries({
        queryKey: ['workspace', workspaceName, 'repos', repoName, 'prs', number, 'labels'],
      });
    },
  });
}
