'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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _path;

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

var _constants;

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

var _bitId;

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

var _repository;

function _load_repository() {
  return _repository = _interopRequireDefault(require('../repository'));
}

var _scope;

function _load_scope() {
  return _scope = _interopRequireDefault(require('../scope'));
}

var _component;

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

var _dependencyMaps;

function _load_dependencyMaps() {
  return _dependencyMaps = require('../dependency-maps');
}

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var External = function (_Repository) {
  _inherits(External, _Repository);

  function External(scope, externalMap) {
    _classCallCheck(this, External);

    var _this = _possibleConstructorReturn(this, (External.__proto__ || Object.getPrototypeOf(External)).call(this, scope));

    _this.externalMap = externalMap;
    return _this;
  }

  _createClass(External, [{
    key: 'getPath',
    value: function getPath() {
      return (_path || _load_path()).default.join(_get(External.prototype.__proto__ || Object.getPrototypeOf(External.prototype), 'getPath', this).call(this), (_constants || _load_constants()).BIT_EXTERNAL_DIRNAME);
    }
  }, {
    key: 'composePath',
    value: function composePath(bitId) {
      return (_path || _load_path()).default.join(bitId.scope, bitId.box, bitId.name, bitId.version);
    }
  }, {
    key: 'store',
    value: function store(bit) {
      var _this2 = this;

      return bit.cd(this.composePath(bit.getId())).write(true).then(function () {
        return _this2.externalMap.setBit(bit);
      }).then(function () {
        return bit;
      });
    }
  }, {
    key: 'storeMany',
    value: function storeMany(bits) {
      return Promise.all(bits.map(function (bit) {
        return bit.store();
      }));
    }
  }, {
    key: 'get',
    value: function get(id) {
      return (_component || _load_component()).default.load(this.composePath(id), id.name).then(function (bit) {
        return { bit, success: true };
      }).catch(function () {
        return { id, success: false };
      });
    }
  }, {
    key: 'write',
    value: function write() {
      return this.externalMap.write();
    }
  }]);

  return External;
}((_repository || _load_repository()).default);

exports.default = External;