"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 _ttyTable() {
  const data = _interopRequireDefault(require("tty-table"));

  _ttyTable = 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 _utils() {
  const data = require("../../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _remoteUndefined() {
  const data = _interopRequireDefault(require("../exceptions/remote-undefined"));

  _remoteUndefined = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

class Remote extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'remote');
    (0, _defineProperty2().default)(this, "description", `manage set of tracked bit scope(s)\n  https://${_constants().BASE_DOCS_DOMAIN}/docs/cli-remote.html`);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['g', 'global', 'see globally configured remotes']]);
    (0, _defineProperty2().default)(this, "migration", true);
    (0, _defineProperty2().default)(this, "commands", [new RemoteAdd(), new RemoteRm()]);
  }

  // $FlowFixMe
  action(args, {
    global
  }) {
    return (0, _consumer().remoteList)(global);
  }

  report(remotes) {
    if ((0, _utils().empty)(remotes)) return _chalk().default.red('no configured remotes found in scope');
    const header = [{
      value: 'scope name',
      width: 30,
      headerColor: 'cyan'
    }, {
      value: 'host',
      width: 100,
      headerColor: 'cyan'
    }];
    const opts = {
      align: 'left'
    };
    const table = new (_ttyTable().default)(header, [], opts);
    (0, _utils().forEach)(remotes, (host, name) => {
      table.push([name, host]);
    });
    return table.render();
  }

}

exports.default = Remote;

class RemoteAdd extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'add <url>');
    (0, _defineProperty2().default)(this, "description", 'add a tracked bit remote');
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['g', 'global', 'configure a remote bit scope']]);
  }

  action([url], {
    global
  }) {
    try {
      if (!url) return Promise.reject(new (_remoteUndefined().default)());
      return (0, _consumer().remoteAdd)(url, global);
    } catch (err) {
      return Promise.reject(err);
    }
  }

  report({
    name,
    host
  }) {
    return _chalk().default.green(`added remote scope '${_chalk().default.bold(name)}' with host '${_chalk().default.bold(host)}'`);
  }

}

class RemoteRm extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'del <name>');
    (0, _defineProperty2().default)(this, "description", 'remove a tracked bit remote');
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['g', 'global', 'remove a global configured remote scope']]);
  }

  action([name], {
    global
  }) {
    return (0, _consumer().remoteRm)(name, global);
  }

  report(name) {
    return _chalk().default.green(`successfully removed remote ${_chalk().default.bold(name)}`);
  }

}