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

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

export function useGitBlob(
  workspaceName: string | null,
  repoName: string | null,
  revision: string | null,
  path: string | null,
) {
  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'git', 'blob', { revision, path }],
    queryFn: () => {
      if (!workspaceName || !repoName || !revision || !path) throw new Error('Missing params');
      return GitService.gitGetBlob({ workspaceName, repoName, revision, path }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && !!repoName && !!revision && !!path,
    staleTime: 1000 * 60 * 2,
  });
}
