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

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

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

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