'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

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 _semver;

function _load_semver() {
  return _semver = _interopRequireDefault(require('semver'));
}

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _bitId;

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

var _repository;

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

var _componentObjects;

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

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../logger/logger'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../error/general-error'));
}

var _exceptions;

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

var _customError;

function _load_customError() {
  return _customError = _interopRequireDefault(require('../error/custom-error'));
}

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

var ComponentVersion = function () {
  function ComponentVersion(component, version) {
    _classCallCheck(this, ComponentVersion);

    this.component = component;
    this.version = version;
  }

  _createClass(ComponentVersion, [{
    key: 'getVersion',
    value: function getVersion(repository) {
      return this.component.loadVersion(this.version, repository);
    }
  }, {
    key: 'flattenedDependencies',
    value: function flattenedDependencies(repository) {
      return this.getVersion(repository).then(function (version) {
        return version.flattenedDependencies;
      });
    }
  }, {
    key: 'flattenedDevDependencies',
    value: function flattenedDevDependencies(repository) {
      return this.getVersion(repository).then(function (version) {
        return version.flattenedDevDependencies;
      });
    }
  }, {
    key: 'flattenedCompilerDependencies',
    value: function flattenedCompilerDependencies(repository) {
      return this.getVersion(repository).then(function (version) {
        return version.flattenedCompilerDependencies;
      });
    }
  }, {
    key: 'flattenedTesterDependencies',
    value: function flattenedTesterDependencies(repository) {
      return this.getVersion(repository).then(function (version) {
        return version.flattenedTesterDependencies;
      });
    }
  }, {
    key: 'toId',
    value: function toId() {
      return new (_bitId || _load_bitId()).BitId({
        scope: this.component.scope,
        name: this.component.name,
        version: this.version
      });
    }
  }, {
    key: 'toConsumer',
    value: function toConsumer(repo, manipulateDirData) {
      // $FlowFixMe
      return this.component.toConsumerComponent(this.version, this.component.scope, repo, manipulateDirData);
    }
  }, {
    key: 'toObjects',
    value: function () {
      var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(repo, clientVersion) {
        var version, _ref2, _ref3, compObject, objects, versionBuffer, scopeMeta, originalVersionHash, currentVersionHash;

        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return this.getVersion(repo);

              case 2:
                version = _context.sent;

                if (version) {
                  _context.next = 5;
                  break;
                }

                throw new (_generalError || _load_generalError()).default(`failed loading version ${this.version} of ${this.component.id()}`);

              case 5:
                if (!(clientVersion && version.overrides && !(_ramda || _load_ramda()).default.isEmpty(version.overrides) && (_semver || _load_semver()).default.lt(clientVersion, '14.1.0'))) {
                  _context.next = 7;
                  break;
                }

                throw new (_customError || _load_customError()).default(`Your components were created with a newer version and use the "overrides" feature.
Please upgrade your bit client to version >= v14.1.0`);

              case 7:
                _context.prev = 7;
                _context.next = 10;
                return Promise.all([this.component.asRaw(repo), version.collectRaw(repo), version.asRaw(repo), repo.getScopeMetaObject()]);

              case 10:
                _ref2 = _context.sent;
                _ref3 = _slicedToArray(_ref2, 4);
                compObject = _ref3[0];
                objects = _ref3[1];
                versionBuffer = _ref3[2];
                scopeMeta = _ref3[3];
                return _context.abrupt('return', new (_componentObjects || _load_componentObjects()).default(compObject, objects.concat([versionBuffer, scopeMeta])));

              case 19:
                _context.prev = 19;
                _context.t0 = _context['catch'](7);

                (_logger || _load_logger()).default.error(_context.t0);
                originalVersionHash = this.component.versions[this.version].toString();
                currentVersionHash = version.hash().toString();

                if (!(originalVersionHash !== currentVersionHash)) {
                  _context.next = 26;
                  break;
                }

                throw new (_exceptions || _load_exceptions()).HashMismatch(this.component.id(), this.version, originalVersionHash, currentVersionHash);

              case 26:
                throw _context.t0;

              case 27:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this, [[7, 19]]);
      }));

      function toObjects(_x, _x2) {
        return _ref.apply(this, arguments);
      }

      return toObjects;
    }()
  }, {
    key: 'id',
    get: function get() {
      return this.toId();
    }
  }]);

  return ComponentVersion;
}();

exports.default = ComponentVersion;