#!/usr/bin/env node
"use strict";

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

var _process = _interopRequireDefault(require("process"));

var _functions = require("../functions");

var _utils = require("../utils");

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

/**
 * Check if the authentication token is valid
 * and save it locally
 * @param {string} token Udacity authentication
 */
async function listNanodegrees() {
  try {
    const token = await (0, _functions.retrieveUdacityAuthToken)();
    const user = await (0, _functions.retrieveUserInfo)(token);
    const {
      nickname,
      nanodegrees
    } = user;
    const graduatedNanodegrees = user.graduated_nanodegrees;

    _utils.logger.info(`Hi ${nickname || user.first_name || 'there'}!`);

    _utils.logger.info('');

    if (!nanodegrees && !graduatedNanodegrees) {
      _utils.logger.info('Your user profile have no Nanodegree lists');

      return;
    } // show user all Nanodegrees


    if (graduatedNanodegrees && graduatedNanodegrees.length) {
      _utils.logger.info('Here\'s a list of your graduated Nanodegrees (list by Nanodegree key only. The API doesn\'t return Nanodegree name so it can\'t be listed here):');

      for (let i = 0, len = graduatedNanodegrees.length; i < len; i += 1) {
        const nd = graduatedNanodegrees[i];

        _utils.logger.info(` - ${nd.key} version ${nd.version} (locale: ${nd.locale})`);
      }

      _utils.logger.info('');
    }

    if (nanodegrees && nanodegrees.length) {
      _utils.logger.info('Here\'s a list of your enrolled Nanodegrees:');

      for (let i = 0, len = nanodegrees.length; i < len; i += 1) {
        const nd = nanodegrees[i];

        _utils.logger.info(` - ${nd.key} version ${nd.version} (locale: ${nd.locale}): ${nd.title || 'Unnamed Nanodegree'}`);
      }
    }
  } catch (error) {
    _utils.logger.error(`Failed to get Nanodegree list with error:
${JSON.stringify(error, null, 4)}`);

    _process.default.exit(1);
  }
}