import { ofetch } from 'ofetch'
import encryptAes from './encryptAes'
import dayjs from 'dayjs'

const API = import.meta.env.VITE_BACKEND

export default async (api: string, body: Object) => {
  const today = new Date()
  const todayUnix = dayjs(today).unix()
  const expiration = todayUnix + 9
  const aes = await encryptAes(`${todayUnix.toString()}-${expiration.toString()}`)

  const hash = `ocbo-${aes}-token`
  try {
    await ofetch(API + api, {
      headers: {
        Accept: 'application/json',
        'Cache-Control': 'no-cache',
        'OCBO-Token': hash,
      },
      method: 'POST',
      body: body,
    })
    return true
  } catch {
    return false
  }
}
