"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

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

  _fsExtra = function () {
    return data;
  };

  return data;
}

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

  _path = function () {
    return data;
  };

  return data;
}

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

  _utils = function () {
    return data;
  };

  return data;
}

function _remotes() {
  const data = require("../remotes");

  _remotes = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = _interopRequireDefault(require("../bit-id/bit-id"));

  _bitId = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

function _exceptions() {
  const data = require("./exceptions");

  _exceptions = function () {
    return data;
  };

  return data;
}

function getPath(scopePath) {
  return _path().default.join(scopePath, _constants().SCOPE_JSON);
}

class ScopeJson {
  constructor({
    name,
    remotes,
    resolverPath,
    license,
    groupName,
    version
  }) {
    this.name = name;
    this.version = version;
    this.resolverPath = resolverPath;
    this.license = license;
    this.remotes = remotes || {};
    this.groupName = groupName || '';
  }

  set name(suggestedName) {
    this._name = _bitId().default.getValidScopeName(suggestedName);
    return this;
  }

  get name() {
    return this._name;
  }

  toPlainObject() {
    return (0, _utils().cleanObject)({
      name: this.name,
      remotes: this.remotes,
      resolverPath: this.resolverPath,
      license: this.license,
      groupName: this.groupName,
      version: this.version
    });
  }

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

  set(key, val) {
    if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`);
    this[key] = val;
    return this;
  }

  get(key) {
    if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`);
    return this[key];
  }

  del(key) {
    if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`);
    return this[key];
  }

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

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

  write(path) {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      return (0, _utils().writeFile)(_path().default.join(path, _constants().SCOPE_JSON), _this.toJson());
    })();
  }

  static loadFromJson(json) {
    return new ScopeJson(JSON.parse(json));
  }

  static loadFromFile(scopeJsonPath) {
    return (0, _bluebird().coroutine)(function* () {
      let rawScopeJson;

      try {
        rawScopeJson = yield _fsExtra().default.readFile(scopeJsonPath);
      } catch (err) {
        if (err.code === 'ENOENT') throw new (_exceptions().ScopeJsonNotFound)(scopeJsonPath);
        throw err;
      }

      return ScopeJson.loadFromJson(rawScopeJson.toString());
    })();
  }

  getPopulatedLicense() {
    if (!this.get('license') || !_fsExtra().default.existsSync(this.get('license'))) return Promise.resolve();
    return _fsExtra().default.readFile(this.get('license')).then(license => license.toString());
  }

}

exports.ScopeJson = ScopeJson;