"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 _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

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

  _scopeComponentsImporter = function () {
    return data;
  };

  return data;
}

function _isolator() {
  const data = _interopRequireDefault(require("../../../environment/isolator"));

  _isolator = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _isolate = (0, _bluebird().coroutine)(function* (componentId, scopePath, opts) {
    if (opts.useCapsule) {
      return isolateUsingCapsule(componentId, opts);
    }

    _logger().default.debugAndAddBreadCrumb('isolate', 'starting isolation process');

    if (opts.verbose) console.log('starting isolation process'); // eslint-disable-line no-console

    let scope; // If a scope path provided we will take the component from that scope

    if (scopePath) {
      scope = yield (0, _scope().loadScope)(scopePath);
      const bitId = yield scope.getParsedId(componentId);
      return isolateComponent(scope, bitId, opts);
    } // If a scope path was not provided we will get the consumer's scope


    const consumer = yield (0, _consumer().loadConsumer)();
    scope = consumer.scope;
    const bitId = consumer.getParsedId(componentId);
    return isolateComponent(scope, bitId, opts);
  });

  function isolate(_x, _x2, _x3) {
    return _isolate.apply(this, arguments);
  }

  return isolate;
}();

exports.default = _default;

function isolateUsingCapsule(_x4, _x5) {
  return _isolateUsingCapsule.apply(this, arguments);
}
/**
 * import a component end to end. Including importing the dependencies and installing the npm
 * packages.
 *
 * @param {BitId} bitId - the component id to isolate
 * @param {IsolateOptions} opts
 * @return {Promise.<string>} - the path to the isolated component
 */


function _isolateUsingCapsule() {
  _isolateUsingCapsule = (0, _bluebird().coroutine)(function* (componentId, opts) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const bitId = consumer.getParsedId(componentId);
    const isolator = yield _isolator().default.getInstance('fs', consumer.scope, consumer, opts.directory);
    return isolator.isolate(bitId, opts);
  });
  return _isolateUsingCapsule.apply(this, arguments);
}

function isolateComponent(_x6, _x7, _x8) {
  return _isolateComponent.apply(this, arguments);
}

function _isolateComponent() {
  _isolateComponent = (0, _bluebird().coroutine)(function* (scope, bitId, opts) {
    const scopeComponentsImporter = _scopeComponentsImporter().default.getInstance(scope);

    const component = yield scopeComponentsImporter.loadComponent(bitId);
    return component.isolate(scope, opts);
  });
  return _isolateComponent.apply(this, arguments);
}