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

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

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

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