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

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

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

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