export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig()
  //const logshipSession = getCookie(event, 'logship_session')
  const logshipToken = getCookie(event, 'logship_it') //await useStorage().getItem('logship_token_'+logshipSession)

  event.context.fetch = async (url: string, method: string = 'GET', token: string, body: any) => {
    let options: any = {}
    if(token || logshipToken) {
      options = {
        ...options,
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
          Authorization: 'Bearer '+(token || logshipToken)
        }
      }
    }
    if(body) {
      options = {
        ...options,
        body: body
      }
    }
    /* 
    // "if you want to see the request body on this endpoint"
    return body 
    */
    /*
    // "if you want to used the javascript fetch api"
    const res: any = await fetch(config.api.urlV1+'/'+url, {
      method: method,
      headers: options['headers'],
      body: JSON.stringify(options['body'])
    }); 
    return await res.json(); 
    */
    /*
    // "try catch for 500 error not 401 unauthorized problem"
    console.log(options)
    if(options?.body?.C_OrderLine) {
      console.log(options.body.C_OrderLine)
    }
    if(options?.body?.C_InvoiceLine) {
      console.log(options.body.C_InvoiceLine)
    }
    try {
      return await $fetch(config.api.urlV1+'/'+url, {
        method: method,
        ...options
      })
    } catch(error: any) {
      console.log(error)
    }
    */
    console.log(options)
    console.log(JSON.stringify(options?.body || {}))
    return await $fetch(config.api.url+'/'+url, {
      method: method,
      ...options
    })
  }
})