import { apiGet } from '@/lib/api'

import type { WikiPage } from '@/types'


export const fetchWikiPages = async (
    { title }:  { title?: string }): Promise<WikiPage[]> =>
    await apiGet ('/wiki', { params: { title } })


export const fetchWikiPage = async (
    id:           string,
    { version }:  { version?: string }): Promise<WikiPage> =>
    await apiGet (`/wiki/${ id }`, { params: version ? { version } : { } })


export const fetchWikiPageByTitle = async (
    title:        string,
    { version }:  { version?: string }): Promise<WikiPage | null> => {
  try
  {
    return await apiGet (`/wiki/title/${ encodeURIComponent (title) }`, { params: { version } })
  }
  catch
  {
    return null
  }
}
