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

import type { CreateIssueReactionParams } from '@/client/models/CreateIssueReactionParams';
import { IssuesService } from '@/client/services/IssuesService';

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

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