"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _scope() {
  const data = require("../../../scope");

  _scope = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _hooks() {
  const data = _interopRequireDefault(require("../../../hooks"));

  _hooks = function () {
    return data;
  };

  return data;
}

function _scopeComponentsImporter() {
  const data = _interopRequireDefault(require("../../../scope/component-ops/scope-components-importer"));

  _scopeComponentsImporter = function () {
    return data;
  };

  return data;
}

// import logger from '../../../logger/logger';
const HooksManagerInstance = _hooks().default.getInstance();

var _default =
/*#__PURE__*/
function () {
  var _fetch = (0, _bluebird().coroutine)(function* (path, ids, noDependencies = false, headers) {
    const bitIds = _bitId().BitIds.deserialize(ids);

    const args = {
      path,
      bitIds,
      noDependencies
    }; // This might be undefined in case of fork process like during bit test command

    if (HooksManagerInstance) {
      HooksManagerInstance.triggerHook(_constants().PRE_SEND_OBJECTS, args, headers);
    }

    const scope = yield (0, _scope().loadScope)(path);

    const scopeComponentsImporter = _scopeComponentsImporter().default.getInstance(scope);

    const importedComponents = noDependencies ? yield scopeComponentsImporter.importManyWithoutDependencies(bitIds, false) : yield scopeComponentsImporter.importMany(bitIds);
    const clientVersion = headers ? headers.version : null;
    const componentObjects = yield scopeComponentsImporter.componentsToComponentsObjects( // $FlowFixMe
    importedComponents, clientVersion);

    if (HooksManagerInstance) {
      yield HooksManagerInstance.triggerHook(_constants().POST_SEND_OBJECTS, {
        componentObjects,
        scopePath: path,
        componentsIds: bitIds.serialize(),
        scopeName: scope.scopeJson.name
      }, headers);
    }

    return componentObjects;
  });

  function fetch(_x, _x2) {
    return _fetch.apply(this, arguments);
  }

  return fetch;
}();

exports.default = _default;