'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 _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');
}

var _hasWildcard;

function _load_hasWildcard() {
  return _hasWildcard = _interopRequireDefault(require('../../../utils/string/has-wildcard'));
}

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

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(componentIds, all) {
    var untrackedComponents, missing, unRemovableComponents, consumer, componentsList, newComponents, idsHaveWildcard, componentsToUntrack;
    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;
            idsHaveWildcard = (0, (_hasWildcard || _load_hasWildcard()).default)(componentIds);

            if (!(all || idsHaveWildcard)) {
              _context.next = 17;
              break;
            }

            componentsToUntrack = (0, (_hasWildcard || _load_hasWildcard()).default)(componentIds) ? (_componentsList || _load_componentsList()).default.filterComponentsByWildcard(newComponents, componentIds) : newComponents;

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

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

          case 17:
            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 = 20;
            return consumer.onDestroy();

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

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

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

  return untrack;
}();