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

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

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

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