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

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

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

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