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

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

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

  return useMutation({
    mutationFn: (branchName: string) =>
      ReposService.repoSetDefaultBranch({ workspaceName, repoName, branchName }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'branches'] });
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName] });
    },
  });
}
