import { z } from 'zod'

import { HttpServer, type THttpServer } from '~/libs/http-server'

const categorySchema = z.object({
  data: z.array(
    z.object({
      id: z.string(),
      code: z.string(),
      name: z.string(),
    }),
  ),
})

export type TCategorySchema = z.infer<typeof categorySchema>

export const getCategories = async (parameters?: THttpServer) => {
  try {
    const { data } = await HttpServer(parameters).get(`/api/category`)
    return categorySchema.parse(data)
  } catch (error) {
    // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
    return Promise.reject(error)
  }
}
