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

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

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

  return useMutation({
    mutationFn: ({ releaseId, ...params }: UpdateReleaseParams & { releaseId: string }) =>
      ReposService.repoUpdateRelease({ workspaceName, repoName, releaseId, requestBody: params }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'releases'] });
    },
  });
}
