#!/usr/bin/env node

/**
 * External dependencies
 */
"use strict";

var _chalk = _interopRequireDefault(require("chalk"));

var _command = _interopRequireWildcard(require("../lib/cli/command"));

var _app = _interopRequireDefault(require("../lib/api/app"));

var _tracker = require("../lib/tracker");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

/**
 * Internal dependencies
 */
(0, _command.default)({
  requiredArgs: 1,
  format: true
}).example('vip app <app>', 'Pass an app name or ID to get details about that app').example('vip app 123', 'Get details about the app with ID 123').example('vip app vip-test', 'Get details about the app named vip-test').command('list', 'List your VIP applications').argv(process.argv, async arg => {
  await (0, _tracker.trackEvent)('app_command_execute');
  let res;

  try {
    res = await (0, _app.default)(arg[0], 'id,repo,name,environments{id,appId,name,type,branch,currentCommit,primaryDomain{name}}');
  } catch (e) {
    await (0, _tracker.trackEvent)('app_command_fetch_error', {
      error: `App ${arg[0]} does not exist`
    });
    console.log(`App ${_chalk.default.blueBright(arg[0])} does not exist`);
    return;
  }

  if (!res || !res.environments) {
    await (0, _tracker.trackEvent)('app_command_fetch_error', {
      error: `App ${arg[0]} does not exist`
    });
    console.log(`App ${_chalk.default.blueBright(arg[0])} does not exist`);
    return;
  }

  await (0, _tracker.trackEvent)('app_command_success'); // Clone the read-only response object so we can modify it

  const r = Object.assign({}, res);
  const header = [{
    key: 'id',
    value: res.id
  }, {
    key: 'name',
    value: res.name
  }, {
    key: 'repo',
    value: res.repo
  }];
  r.environments = r.environments.map(env => {
    const e = Object.assign({}, env);
    e.name = (0, _command.getEnvIdentifier)(env); // Use the short version of git commit hash

    e.currentCommit = e.currentCommit.substring(0, 7); // Flatten object

    e.primaryDomain = e.primaryDomain.name;
    return e;
  });
  return {
    header,
    data: r.environments
  };
});