"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../api/consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

class Login extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'login');
    (0, _defineProperty2().default)(this, "description", 'log the CLI into Bit');
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['p', 'port <port>', 'port number to open for localhost server (default 8085)'], ['', 'suppress-browser-launch', 'do not open a browser for authentication'], ['', 'npmrc-path <path>', `path to npmrc file to configure ${_constants().BASE_WEB_DOMAIN} registry`], ['', 'skip-registry-config [boolean]', `don't configure ${_constants().BASE_WEB_DOMAIN} registry`], ['', 'machine-name <string>', 'specify machine-name to pair with the token (useful for CI to avoid accidentally revoke the token)']]);
  }

  // $FlowFixMe
  action([], {
    port,
    suppressBrowserLaunch = false,
    npmrcPath,
    skipRegistryConfig = false,
    machineName
  }) {
    return (0, _consumer().login)(port, suppressBrowserLaunch, npmrcPath, skipRegistryConfig, machineName).then(({
      isAlreadyLoggedIn,
      username,
      npmrcPath,
      writeToNpmrcError
    }) => ({
      isAlreadyLoggedIn,
      username,
      npmrcPath,
      writeToNpmrcError,
      skipRegistryConfig
    }));
  }

  report({
    isAlreadyLoggedIn = false,
    username,
    npmrcPath,
    skipRegistryConfig,
    writeToNpmrcError
  }) {
    if (isAlreadyLoggedIn) return _chalk().default.yellow('already logged in');

    const successLoginMessage = _chalk().default.green(`success! logged in as ${username}`);

    let writeToNpmrcMessage = '\n';

    if (!skipRegistryConfig) {
      writeToNpmrcMessage = writeToNpmrcError ? _chalk().default.yellow(`\nunable to add @bit as a scoped registry at "${_chalk().default.bold(npmrcPath)}"\n`) : _chalk().default.green(`\nsuccessfully added @bit as a scoped registry at ${npmrcPath}\n`);
    }

    const finalMessage = `${writeToNpmrcMessage}${successLoginMessage}`;
    return finalMessage;
  }

}

exports.default = Login;