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

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

export function useGitCommits(
  workspaceName: string | null,
  repoName: string | null,
  revision?: string,
  path?: string,
  pageSize = 30,
) {
  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'git', 'commits', { revision, path, pageSize }],
    queryFn: () => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return GitService.gitListCommits({
        workspaceName,
        repoName,
        revision: revision ?? 'HEAD',
        path: path ?? undefined,
        pageSize,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName && !!repoName,
    staleTime: 1000 * 60,
  });
}
