const { Corellium } = require('@corellium/corellium-api')
const { readProfile } = require('./profile')

let corellium

/**
 * Get an authenticated instance of @corellium/corellium-api
 * @returns {Corellium} corellium
 */
async function getCorelliumApi () {
  if (!corellium) {
    const profile = readProfile()
    const { endpoint, token } = profile
    const client = new Corellium({ endpoint, token: { token } })
    await client.login()

    corellium = client
  }

  return corellium
}

module.exports = {
  getCorelliumApi
}
