import { z } from 'zod'

import { type TLoginSchema } from '~/layouts/news/form-login'
import HttpClient from '~/libs/http-client'

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

export const newsLoginRequest = async (payload: TLoginSchema) => {
  try {
    const { data } = await HttpClient().post('/api/user/login', payload)
    return loginResponseSchema.parse(data)
  } catch (error) {
    // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
    return Promise.reject(error)
  }
}
