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

import type { ForkRepoParams } from '@/client/models/ForkRepoParams';
import { ReposService } from '@/client/services/ReposService';

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

  return useMutation({
    mutationFn: (params: ForkRepoParams) => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return ReposService.repoFork({ workspaceName, repoName, requestBody: params });
    },
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'forks'] });
    },
  });
}
