"use strict";

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

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

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _utils = function () {
    return data;
  };

  return data;
}

function _network() {
  const data = require("../scope/network");

  _network = function () {
    return data;
  };

  return data;
}

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

  _exceptions = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

/**
 * @ctx bit, primary, remote
 */
function isPrimary(alias) {
  return alias.includes('!');
}

class Remote {
  constructor(host, name, primary = false) {
    (0, _defineProperty2().default)(this, "primary", false);
    this.name = name || '';
    this.host = host;
    this.primary = primary;
  }

  connect() {
    return (0, _network().connect)(this.host);
  }

  toPlainObject() {
    return {
      host: this.host,
      name: this.name
    };
  }

  scope() {
    return this.connect().then(network => {
      return network.describeScope();
    });
  }

  list(namespacesUsingWildcards) {
    return this.connect().then(network => network.list(namespacesUsingWildcards));
  }

  search(query, reindex) {
    return this.connect().then(network => network.search(query, reindex));
  }

  show(bitId) {
    return this.connect().then(network => network.show(bitId));
  }

  fetch(bitIds, withoutDeps, context) {
    return this.connect().then(network => network.fetch(bitIds, withoutDeps, context));
  }

  latestVersions(bitIds) {
    return this.connect().then(network => network.latestVersions(bitIds));
  }

  validate() {
    if (!(0, _utils().isBitUrl)(this.host)) throw new (_exceptions().InvalidRemote)();
  }

  push(componentObjects) {
    return (0, _network().connect)(this.host).then(network => network.push(componentObjects));
  }

  pushMany(components, context) {
    return (0, _network().connect)(this.host).then(network => network.pushMany(components, context));
  }

  deleteMany(ids, force, context) {
    return (0, _network().connect)(this.host).then(network => network.deleteMany(ids, force, context));
  }

  deprecateMany(ids, context) {
    return (0, _network().connect)(this.host).then(network => network.deprecateMany(ids, context));
  }

  undeprecateMany(ids, context) {
    return (0, _network().connect)(this.host).then(network => network.undeprecateMany(ids, context));
  }

  static load(name, host) {
    const primary = isPrimary(name);
    if (primary) name = (0, _utils().cleanBang)(name);
    return new Remote(name, host, primary);
  }

}

exports.default = Remote;