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

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

export function useCreateIssue(workspaceName: string | null) {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: (params: CreateIssueParams) => {
      if (!workspaceName) throw new Error('No workspace name');
      return IssuesService.issueCreate({ workspaceName, requestBody: params });
    },
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['workspace', workspaceName, 'issues'] });
    },
  });
}
