'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var remove = exports.remove = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(name, global) {
    var globalRemotes, _hasRemoved, scope, hasRemoved;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!global) {
              _context.next = 10;
              break;
            }

            _context.next = 3;
            return (_globalConfig || _load_globalConfig()).GlobalRemotes.load();

          case 3:
            globalRemotes = _context.sent;
            _hasRemoved = globalRemotes.rmRemote(name);

            if (_hasRemoved) {
              _context.next = 7;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`remote "${name}" was not found globally, to remove a local remote, please omit the "--global" flag`);

          case 7:
            _context.next = 9;
            return globalRemotes.write();

          case 9:
            return _context.abrupt('return', name);

          case 10:
            _context.next = 12;
            return (0, (_scope || _load_scope()).loadScope)();

          case 12:
            scope = _context.sent;
            hasRemoved = scope.scopeJson.rmRemote(name);

            if (hasRemoved) {
              _context.next = 16;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`remote "${name}" was not found locally, to remove a global remote, please use "--global" flag`);

          case 16:
            _context.next = 18;
            return scope.scopeJson.write(scope.getPath());

          case 18:
            return _context.abrupt('return', name);

          case 19:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function remove(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

exports.add = add;
exports.list = list;
exports.refresh = refresh;

var _scope;

function _load_scope() {
  return _scope = require('../../../scope');
}

var _remotes;

function _load_remotes() {
  return _remotes = require('../../../remotes');
}

var _globalConfig;

function _load_globalConfig() {
  return _globalConfig = require('../../../global-config');
}

var _scopeRemotes;

function _load_scopeRemotes() {
  return _scopeRemotes = require('../../../scope/scope-remotes');
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../../error/general-error'));
}

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

function buildRemote(url) {
  return new (_remotes || _load_remotes()).Remote(url);
}
function add(url, global) {
  var remote = buildRemote(url);
  return remote.scope().then(function (scopeDesc) {
    remote.name = scopeDesc.name;

    if (global) {
      return (_globalConfig || _load_globalConfig()).GlobalRemotes.load().then(function (globalRemotes) {
        return globalRemotes.addRemote(remote).write().then(function () {
          return remote;
        });
      });
    }

    return (0, (_scope || _load_scope()).loadScope)().then(function (scope) {
      return scope.scopeJson.addRemote(remote).write(scope.getPath()).then(function () {
        return remote;
      });
    });
  });
}

function list(global) {
  if (global) {
    return (_globalConfig || _load_globalConfig()).GlobalRemotes.load().then(function (globalRemotes) {
      return globalRemotes.toPlainObject();
    });
  }

  return (0, (_scope || _load_scope()).loadScope)().then(function (scope) {
    return (0, (_scopeRemotes || _load_scopeRemotes()).getScopeRemotes)(scope).then(function (remotes) {
      return remotes.toPlainObject();
    });
  });
}

function refresh() {}