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

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

export function useRepoRelease(
  workspaceName: string | null,
  repoName: string | null,
  releaseId: string | null,
) {
  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'releases', releaseId],
    queryFn: () => {
      if (!workspaceName || !repoName || !releaseId) throw new Error('Missing params');
      return ReposService.repoGetRelease({ workspaceName, repoName, releaseId }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && !!repoName && !!releaseId,
    staleTime: 1000 * 60 * 2,
  });
}
