'use strict';

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

var _regenerator;

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

var _scope;

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

var _bitId;

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

var _constants;

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

var _hooks;

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

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

// import logger from '../../../logger/logger';

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

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(path, ids) {
    var noDependencies = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var headers = arguments[3];
    var bitIds, args, scope, componentObjects;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            bitIds = (_bitId || _load_bitId()).BitIds.deserialize(ids);
            args = { path, bitIds, noDependencies };
            // This might be undefined in case of fork process like during bit test command

            if (HooksManagerInstance) {
              HooksManagerInstance.triggerHook((_constants || _load_constants()).PRE_SEND_OBJECTS, args, headers);
            }
            _context.next = 5;
            return (0, (_scope || _load_scope()).loadScope)(path);

          case 5:
            scope = _context.sent;

            if (!noDependencies) {
              _context.next = 12;
              break;
            }

            _context.next = 9;
            return scope.manyOneObjects(bitIds);

          case 9:
            _context.t0 = _context.sent;
            _context.next = 15;
            break;

          case 12:
            _context.next = 14;
            return scope.getObjects(bitIds);

          case 14:
            _context.t0 = _context.sent;

          case 15:
            componentObjects = _context.t0;

            if (!HooksManagerInstance) {
              _context.next = 19;
              break;
            }

            _context.next = 19;
            return HooksManagerInstance.triggerHook((_constants || _load_constants()).POST_SEND_OBJECTS, {
              componentObjects,
              scopePath: path,
              componentsIds: bitIds.serialize(),
              scopeName: scope.scopeJson.name
            }, headers);

          case 19:
            return _context.abrupt('return', componentObjects);

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

  function fetch(_x2, _x3) {
    return _ref.apply(this, arguments);
  }

  return fetch;
}();