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

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

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