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

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

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

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