import { stringify } from 'qs-esm'
import { PaginatedDocs } from 'payload'
import { Parish } from '@/payload-types'

export async function fetchParish(slug: string): Promise<PaginatedDocs<Parish> | undefined> {
  const query = {
    slug: {
      equals: slug,
    },
  }

  const stringifiedQuery = stringify(
    {
      where: query,
    },
    { addQueryPrefix: true },
  )

  const res = await fetch(`http://localhost:3000/api/parish${stringifiedQuery}`)
  if (!res.ok) return undefined
  return res.json()
}