import { useToNumber, useToString } from '@vueuse/core'

export default defineEventHandler(async (event) => {
  const query = getQuery(event)
  const limit = useToNumber(useToString(query.limit).value).value || 5
  const search = useToString(query.search)

  try {
    const countries = await prisma.addressCountries.findMany({
      // where: {
      //   Name: {
      //     startsWith: search.value,
      //     mode: 'insensitive',
      //   },
      // },
      // take: limit,
    })
    return countries.map((c) => {
      return {
        Code: c.Code,
        Name: c.Name,
      }
    })
  } catch (error) {
    // Return error if fetching users fails
    return {
      status: 500,
      body: { message: 'Failed to fetch countries' },
    }
  }
})
