'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 isolateUsingCapsule = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(componentId, opts) {
    var consumer, bitId, isolator;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context2.sent;
            bitId = consumer.getParsedId(componentId);
            _context2.next = 6;
            return (_isolator || _load_isolator()).default.getInstance('fs', consumer.scope, consumer, opts.directory);

          case 6:
            isolator = _context2.sent;
            return _context2.abrupt('return', isolator.isolate(bitId, opts));

          case 8:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  return function isolateUsingCapsule(_x4, _x5) {
    return _ref2.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
 */


var isolateComponent = function () {
  var _ref3 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(scope, bitId, opts) {
    var scopeComponentsImporter, component;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            scopeComponentsImporter = (_scopeComponentsImporter || _load_scopeComponentsImporter()).default.getInstance(scope);
            _context3.next = 3;
            return scopeComponentsImporter.loadComponent(bitId);

          case 3:
            component = _context3.sent;
            return _context3.abrupt('return', component.isolate(scope, opts));

          case 5:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this);
  }));

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

var _scope;

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

var _consumer;

function _load_consumer() {
  return _consumer = require('../../../consumer');
}

var _logger;

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

var _bitId2;

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

var _scopeComponentsImporter;

function _load_scopeComponentsImporter() {
  return _scopeComponentsImporter = _interopRequireDefault(require('../../../scope/component-ops/scope-components-importer'));
}

var _isolator;

function _load_isolator() {
  return _isolator = _interopRequireDefault(require('../../../environment/isolator'));
}

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

// TODO: merge this with other instances of the same options
exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(componentId, scopePath, opts) {
    var scope, _bitId, consumer, bitId;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!opts.useCapsule) {
              _context.next = 2;
              break;
            }

            return _context.abrupt('return', isolateUsingCapsule(componentId, opts));

          case 2:
            (_logger || _load_logger()).default.debugAndAddBreadCrumb('isolate', 'starting isolation process');
            if (opts.verbose) console.log('starting isolation process'); // eslint-disable-line no-console
            scope = void 0;
            // If a scope path provided we will take the component from that scope

            if (!scopePath) {
              _context.next = 13;
              break;
            }

            _context.next = 8;
            return (0, (_scope || _load_scope()).loadScope)(scopePath);

          case 8:
            scope = _context.sent;
            _context.next = 11;
            return scope.getParsedId(componentId);

          case 11:
            _bitId = _context.sent;
            return _context.abrupt('return', isolateComponent(scope, _bitId, opts));

          case 13:
            _context.next = 15;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 15:
            consumer = _context.sent;

            scope = consumer.scope;
            bitId = consumer.getParsedId(componentId);
            return _context.abrupt('return', isolateComponent(scope, bitId, opts));

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

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

  return isolate;
}();