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

function _load_component() {
  return _component = _interopRequireDefault(require('../../../consumer/component'));
}

var _models;

function _load_models() {
  return _models = require('../../../scope/models');
}

var _analytics;

function _load_analytics() {
  return _analytics = require('../../../analytics/analytics');
}

var _loader;

function _load_loader() {
  return _loader = _interopRequireDefault(require('../../../cli/loader'));
}

var _loaderMessages;

function _load_loaderMessages() {
  return _loaderMessages = require('../../../cli/loader/loader-messages');
}

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() {
    var consumer, componentsList, newAndImportPendingComponents, newComponents, importPendingComponents, modifiedComponent, stagedComponents, autoTagPendingComponents, autoTagPendingComponentsStr, invalidComponents, outdatedComponents, newAndModified, componentsWithMissingDeps;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            (_loader || _load_loader()).default.start((_loaderMessages || _load_loaderMessages()).BEFORE_STATUS);
            _context.next = 3;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 3:
            consumer = _context.sent;
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            _context.next = 7;
            return componentsList.listNewComponentsAndImportPending();

          case 7:
            newAndImportPendingComponents = _context.sent;
            newComponents = newAndImportPendingComponents.newComponents, importPendingComponents = newAndImportPendingComponents.importPendingComponents;
            _context.next = 11;
            return componentsList.listModifiedComponents(true);

          case 11:
            modifiedComponent = _context.sent;
            _context.next = 14;
            return componentsList.listExportPendingComponents();

          case 14:
            stagedComponents = _context.sent;
            _context.next = 17;
            return componentsList.listAutoTagPendingComponents();

          case 17:
            autoTagPendingComponents = _context.sent;
            autoTagPendingComponentsStr = autoTagPendingComponents.map(function (component) {
              return component.id().toString();
            });
            _context.next = 21;
            return componentsList.listInvalidComponents();

          case 21:
            invalidComponents = _context.sent;
            _context.next = 24;
            return componentsList.listOutdatedComponents();

          case 24:
            outdatedComponents = _context.sent;


            // Run over the components to check if there is missing dependencies
            newAndModified = newComponents.concat(modifiedComponent);
            componentsWithMissingDeps = newAndModified.filter(function (component) {
              return Boolean(component.issues);
            });

            (_analytics || _load_analytics()).Analytics.setExtraData('new_components', newComponents.length);
            (_analytics || _load_analytics()).Analytics.setExtraData('staged_components', stagedComponents.length);
            (_analytics || _load_analytics()).Analytics.setExtraData('num_components_with_missing_dependencies', componentsWithMissingDeps.length);
            (_analytics || _load_analytics()).Analytics.setExtraData('autoTagPendingComponents', autoTagPendingComponents.length);
            (_analytics || _load_analytics()).Analytics.setExtraData('deleted', invalidComponents.length);
            _context.next = 34;
            return consumer.onDestroy();

          case 34:
            return _context.abrupt('return', {
              newComponents: (_componentsList || _load_componentsList()).default.sortComponentsByName(newComponents),
              modifiedComponent: (_componentsList || _load_componentsList()).default.sortComponentsByName(modifiedComponent),
              stagedComponents: (_componentsList || _load_componentsList()).default.sortComponentsByName(stagedComponents),
              componentsWithMissingDeps, // no need to sort, we don't print it as is
              importPendingComponents, // no need to sort, we use only its length
              autoTagPendingComponents: (_componentsList || _load_componentsList()).default.sortComponentsByName(autoTagPendingComponentsStr),
              invalidComponents,
              outdatedComponents
            });

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

  function status() {
    return _ref.apply(this, arguments);
  }

  return status;
}();