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

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

var _logger;

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

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"); }); }; }

// TODO: merge this with other instances of the same options
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__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:
            (_logger || _load_logger()).default.debug('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 = 11;
              break;
            }

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

          case 6:
            scope = _context.sent;
            _context.next = 9;
            return scope.getParsedId(componentId);

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

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

          case 13:
            consumer = _context.sent;

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

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

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

  return isolate;
}();