'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

var _utils;

function _load_utils() {
  return _utils = require('../utils');
}

var _remotes;

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

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var GlobalRemotes = function () {
  function GlobalRemotes(remotes) {
    _classCallCheck(this, GlobalRemotes);

    this.remotes = remotes;
  }

  _createClass(GlobalRemotes, [{
    key: 'addRemote',
    value: function addRemote(remote) {
      this.remotes[remote.name] = remote.host;
      return this;
    }
  }, {
    key: 'rmRemote',
    value: function rmRemote(name) {
      delete this.remotes[name];
      return this;
    }
  }, {
    key: 'toJson',
    value: function toJson() {
      var readable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

      if (!readable) return JSON.stringify(this.toPlainObject());
      return JSON.stringify(this.toPlainObject(), null, 4);
    }
  }, {
    key: 'asRemotes',
    value: function asRemotes() {
      return (_remotes || _load_remotes()).Remotes.load(this.remotes);
    }
  }, {
    key: 'toPlainObject',
    value: function toPlainObject() {
      return this.remotes;
    }
  }, {
    key: 'write',
    value: function write() {
      return (0, (_utils || _load_utils()).writeFile)((_path || _load_path()).default.join((_constants || _load_constants()).GLOBAL_CONFIG, (_constants || _load_constants()).GLOBAL_REMOTES), this.toJson());
    }
  }], [{
    key: 'load',
    value: function load() {
      return (0, (_utils || _load_utils()).readFile)((_path || _load_path()).default.join((_constants || _load_constants()).GLOBAL_CONFIG, (_constants || _load_constants()).GLOBAL_REMOTES)).then(function (contents) {
        return new GlobalRemotes(JSON.parse(contents.toString('utf8')));
      }).catch(function (err) {
        if (err.code !== 'ENOENT') return err;
        var globalRemotes = new GlobalRemotes({});
        return globalRemotes.write().then(function () {
          return globalRemotes;
        });
      });
    }
  }]);

  return GlobalRemotes;
}();

exports.default = GlobalRemotes;