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

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

var _componentObjects;

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

var _constants;

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

var _hooks;

function _load_hooks() {
  return _hooks = _interopRequireDefault(require('../../../hooks'));
}

var _exportScopeComponents;

function _load_exportScopeComponents() {
  return _exportScopeComponents = require('../../../scope/component-ops/export-scope-components');
}

var _bitIds;

function _load_bitIds() {
  return _bitIds = _interopRequireDefault(require('../../../bit-id/bit-ids'));
}

var _checkVersionCompatibility;

function _load_checkVersionCompatibility() {
  return _checkVersionCompatibility = require('../../../scope/network/check-version-compatibility');
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var HooksManagerInstance = (_hooks || _load_hooks()).default.getInstance();

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref2, headers) {
    var path = _ref2.path,
        componentObjects = _ref2.componentObjects;
    var scope, clientIsOld, componentsBitIds, componentsIds;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (typeof componentObjects === 'string') {
              componentObjects = (_componentObjects || _load_componentObjects()).default.manyFromString(componentObjects);
            }

            _context.next = 3;
            return HooksManagerInstance.triggerHook((_constants || _load_constants()).PRE_RECEIVE_OBJECTS, { path, componentObjects }, headers);

          case 3:
            _context.next = 5;
            return (0, (_scope || _load_scope()).loadScope)(path);

          case 5:
            scope = _context.sent;

            // @todo: remove this once v15 is out.
            clientIsOld = Boolean(headers && headers.version && (0, (_checkVersionCompatibility || _load_checkVersionCompatibility()).isClientHasVersionBefore)('14.1.1', headers.version));
            _context.next = 9;
            return (0, (_exportScopeComponents || _load_exportScopeComponents()).exportManyBareScope)(scope, componentObjects, clientIsOld);

          case 9:
            componentsBitIds = _context.sent;
            componentsIds = componentsBitIds.map(function (id) {
              return id.toString();
            });
            _context.next = 13;
            return HooksManagerInstance.triggerHook((_constants || _load_constants()).POST_RECEIVE_OBJECTS, {
              componentObjects,
              componentsIds,
              scopePath: path,
              scopeName: scope.scopeJson.name
            }, headers);

          case 13:
            return _context.abrupt('return', componentsIds);

          case 14:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

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

  return put;
}();