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

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

export function useRepoReleases(
  workspaceName: string | null,
  repoName: string | null,
  limit = 20,
  offset = 0,
) {
  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'releases', { limit, offset }],
    queryFn: () => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return ReposService.repoListReleases({ workspaceName, repoName, limit, offset }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && !!repoName,
  });
}
