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

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

export function useIssueEvents(workspaceName: string | null, number: number | null) {
  return useQuery({
    queryKey: ['workspace', workspaceName, 'issues', number, 'events'],
    queryFn: () => {
      if (!workspaceName || number === null) throw new Error('Missing params');
      return IssuesService.issueListEvents({ workspaceName, number, limit: 100 }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && number !== null,
    staleTime: 1000 * 60 * 2,
  });
}
