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

import { WikiService } from '@/client/services/WikiService';
import type { UpdateWikiPageParams } from '@/client/models/UpdateWikiPageParams';

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

  return useMutation({
    mutationFn: ({ slug, ...params }: UpdateWikiPageParams & { slug: string }) => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return WikiService.wikiUpdatePage({
        workspaceName,
        repoName,
        slug,
        requestBody: params,
      });
    },
    onSuccess: () => {
      qc.invalidateQueries({
        queryKey: ['workspace', workspaceName, 'repos', repoName, 'wiki'],
      });
    },
  });
}
