import { useMutation, useQueryClient } from '@tanstack/react-query';

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

export function useUnsubscribeIssue(workspaceName: string | null, number: number | null) {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: () => {
      if (!workspaceName || number === null) throw new Error('Missing params');
      return IssuesService.issueUnsubscribe({ workspaceName, number });
    },
    onSuccess: () => {
      qc.invalidateQueries({
        queryKey: ['workspace', workspaceName, 'issues', number, 'subscribers'],
      });
    },
  });
}
