'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 _utils;

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

var _componentObjects;

function _load_componentObjects() {
  return _componentObjects = _interopRequireDefault(require('../scope/component-objects'));
}

var _network;

function _load_network() {
  return _network = require('../scope/network');
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('./exceptions');
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../bit-id');
}

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../consumer/component'));
}

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"); } }

/**
 * @ctx bit, primary, remote
 */
function isPrimary(alias) {
  return (0, (_utils || _load_utils()).contains)(alias, '!');
}

var Remote = function () {
  function Remote(host, name) {
    var primary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

    _classCallCheck(this, Remote);

    this.primary = false;

    this.name = name || '';
    this.host = host;
    this.primary = primary;
  }

  _createClass(Remote, [{
    key: 'connect',
    value: function connect() {
      return (0, (_network || _load_network()).connect)(this.host);
    }
  }, {
    key: 'toPlainObject',
    value: function toPlainObject() {
      return {
        host: this.host,
        name: this.name
      };
    }
  }, {
    key: 'scope',
    value: function scope() {
      return this.connect().then(function (network) {
        return network.describeScope();
      });
    }
  }, {
    key: 'list',
    value: function list() {
      return this.connect().then(function (network) {
        return network.list();
      });
    }
  }, {
    key: 'search',
    value: function search(query, reindex) {
      return this.connect().then(function (network) {
        return network.search(query, reindex);
      });
    }
  }, {
    key: 'show',
    value: function show(bitId) {
      return this.connect().then(function (network) {
        return network.show(bitId);
      });
    }
  }, {
    key: 'fetch',
    value: function fetch(bitIds, withoutDeps, context) {
      return this.connect().then(function (network) {
        return network.fetch(bitIds, withoutDeps, context);
      });
    }
  }, {
    key: 'latestVersions',
    value: function latestVersions(bitIds) {
      return this.connect().then(function (network) {
        return network.latestVersions(bitIds);
      });
    }
  }, {
    key: 'validate',
    value: function validate() {
      if (!(0, (_utils || _load_utils()).isBitUrl)(this.host)) throw new (_exceptions || _load_exceptions()).InvalidRemote();
    }
  }, {
    key: 'push',
    value: function push(componentObjects) {
      return (0, (_network || _load_network()).connect)(this.host).then(function (network) {
        return network.push(componentObjects);
      });
    }
  }, {
    key: 'pushMany',
    value: function pushMany(components, context) {
      return (0, (_network || _load_network()).connect)(this.host).then(function (network) {
        return network.pushMany(components, context);
      });
    }
  }, {
    key: 'deleteMany',
    value: function deleteMany(ids, force, context) {
      return (0, (_network || _load_network()).connect)(this.host).then(function (network) {
        return network.deleteMany(ids, force, context);
      });
    }
  }, {
    key: 'deprecateMany',
    value: function deprecateMany(ids, context) {
      return (0, (_network || _load_network()).connect)(this.host).then(function (network) {
        return network.deprecateMany(ids, context);
      });
    }
  }], [{
    key: 'load',
    value: function load(name, host) {
      var primary = isPrimary(name);
      if (primary) name = (0, (_utils || _load_utils()).cleanBang)(name);

      return new Remote(name, host, primary);
    }
  }]);

  return Remote;
}();

exports.default = Remote;