import { useQuery } from '@tanstack/react-query';

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

export function usePrCheckRuns(
  workspaceName: string | null,
  repoName: string | null,
  number: number | null,
) {
  return useQuery({
    queryKey: ['workspace', workspaceName, 'repos', repoName, 'prs', number, 'check-runs'],
    queryFn: () => {
      if (!workspaceName || !repoName || number === null) throw new Error('Missing params');
      return PullRequestsService.prListCheckRuns({ workspaceName, repoName, number }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && !!repoName && number !== null,
    staleTime: 1000 * 60,
  });
}
