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

import type { CreatePrParams } from '@/client/models/CreatePrParams';
import { PullRequestsService } from '@/client/services/PullRequestsService';

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

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