import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate, useParams } from 'react-router-dom';

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

export function useDeleteRepo() {
  const { workspaceName = '', repoName = '' } = useParams();
  const qc = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationFn: () => ReposService.repoDelete({ workspaceName, repoName }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['workspace', workspaceName, 'repos'] });
      navigate(`/${workspaceName}/repos`);
    },
  });
}
