"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = retrieveUserInfo;

var _ora = _interopRequireDefault(require("ora"));

var _api = require("../../api");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Retrieve user information
 * @param {string} token Udacity authentication token
 */
async function retrieveUserInfo(token) {
  const spinner = (0, _ora.default)('Fetch user information via Udacity API');

  try {
    spinner.start();
    const res = await (0, _api.fetchUdacityUserInfo)(token);
    spinner.succeed();

    if (!res.data || !res.data.user) {
      spinner.fail();
      throw new Error(`Could not fetch user information with error:
      ${res.body}`);
    }

    return res.data.user;
  } catch (error) {
    spinner.fail();
    throw error;
  }
}