'use strict';

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

var _regenerator;

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

var _consumer;

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

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('../../../consumer/component/components-list'));
}

var _bitId;

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

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

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(componentIds, all) {
    var untrackedComponents, missing, unRemovableComponents, consumer, componentsList, newComponents;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            untrackedComponents = [];
            missing = [];
            unRemovableComponents = [];
            _context.next = 5;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 5:
            consumer = _context.sent;
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            // $FlowFixMe

            _context.next = 9;
            return componentsList.listNewComponents(false);

          case 9:
            newComponents = _context.sent;

            if (!all) {
              _context.next = 15;
              break;
            }

            newComponents.forEach(function (componentId) {
              return consumer.bitMap.removeComponent(componentId);
            });
            _context.next = 14;
            return consumer.onDestroy();

          case 14:
            return _context.abrupt('return', { untrackedComponents: newComponents, unRemovableComponents, missingComponents: missing });

          case 15:
            componentIds.forEach(function (componentId) {
              var bitId = consumer.getParsedIdIfExist(componentId);
              if (!bitId) {
                missing.push(componentId);
                return;
              }
              if (newComponents.has(bitId)) {
                untrackedComponents.push(bitId);
                consumer.bitMap.removeComponent(bitId);
              } else {
                unRemovableComponents.push(bitId);
              }
            });
            _context.next = 18;
            return consumer.onDestroy();

          case 18:
            return _context.abrupt('return', { untrackedComponents, unRemovableComponents, missingComponents: missing });

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

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

  return untrack;
}();