"use strict";

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

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

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("../utils");

  _utils = function () {
    return data;
  };

  return data;
}

class GlobalRemotes {
  constructor(remotes) {
    this.remotes = remotes;
  }

  addRemote(remote) {
    this.remotes[remote.name] = remote.host;
    return this;
  }

  rmRemote(name) {
    if (!this.remotes[name]) return false;
    delete this.remotes[name];
    return true;
  }

  toJson(readable = true) {
    if (!readable) return JSON.stringify(this.toPlainObject());
    return JSON.stringify(this.toPlainObject(), null, 4);
  }

  toPlainObject() {
    return this.remotes;
  }

  write() {
    return (0, _utils().writeFile)(_path().default.join(_constants().GLOBAL_CONFIG, _constants().GLOBAL_REMOTES), this.toJson());
  }

  static load() {
    return _fsExtra().default.readFile(_path().default.join(_constants().GLOBAL_CONFIG, _constants().GLOBAL_REMOTES)).then(contents => new GlobalRemotes(JSON.parse(contents.toString('utf8')))).catch(err => {
      if (err.code !== 'ENOENT') return err;
      const globalRemotes = new GlobalRemotes({});
      return globalRemotes.write().then(() => globalRemotes);
    });
  }

}

exports.default = GlobalRemotes;