import { useQuery } from '@tanstack/react-query';

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

export function useWikiPages(
  workspaceName: string | null,
  repoName: string | null,
  search?: string | null,
) {
  return useQuery({
    queryKey: ['workspace', workspaceName, 'repos', repoName, 'wiki', { search }],
    queryFn: () => {
      if (!workspaceName || !repoName) throw new Error('Missing params');
      return WikiService.wikiListPages({
        workspaceName,
        repoName,
        search: search ?? undefined,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName && !!repoName,
    staleTime: 1000 * 60 * 2,
  });
}
