'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.add = add;
exports.remove = remove;
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');
}

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 remove(name, global) {
  if (global) {
    return (_globalConfig || _load_globalConfig()).GlobalRemotes.load().then(function (globalRemotes) {
      return globalRemotes.rmRemote(name).write().then(function () {
        return name;
      });
    });
  }

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

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 scope.remotes().then(function (remotes) {
      return remotes.toPlainObject();
    });
  });
}

function refresh() {}