import { z } from 'zod'

import type { TRegisterSchema } from '~/layouts/news/form-register'
import { HttpServer } from '~/libs/http-server'

const loginResponseSchema = z.object({
  data: z.object({
    token: z.string(),
  }),
})

export const userRegisterRequest = async (payload: TRegisterSchema) => {
  try {
    const { subscribe_plan, ...restPayload } = payload
    const transformedPayload = {
      ...restPayload,
      subscribe_plan_id: subscribe_plan,
    }
    const { data } = await HttpServer().post(
      '/api/user/register',
      transformedPayload,
    )
    return loginResponseSchema.parse(data)
  } catch (error) {
    // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
    return Promise.reject(error)
  }
}
