'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addMany = exports.addOne = undefined;

var _regenerator;

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

var addOne = exports.addOne = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(addProps) {
    var consumer, addContext, addComponents, addResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context.sent;
            addContext = { consumer };
            addComponents = new (_addComponents || _load_addComponents()).default(addContext, addProps);
            _context.next = 7;
            return addComponents.add();

          case 7:
            addResults = _context.sent;
            _context.next = 10;
            return consumer.onDestroy();

          case 10:
            return _context.abrupt('return', addResults);

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

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

var addMany = exports.addMany = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(components, alternateCwd) {
    var consumerPath, consumer, addContext, addComponentsArr, addResults;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            // we are checking whether the consumer is the default consumer which is process.cwd() or it is overriden , and we are working on another directory which is not the process.cwd()
            consumerPath = alternateCwd || process.cwd();
            _context3.next = 3;
            return (0, (_consumer || _load_consumer()).loadConsumer)(consumerPath);

          case 3:
            consumer = _context3.sent;
            addContext = { consumer, alternateCwd: consumerPath };
            addComponentsArr = [];

            components.forEach(function (component) {
              var normalizedPaths = component.componentPaths.map(function (p) {
                return (_path || _load_path()).default.normalize(p);
              });
              component.componentPaths = normalizedPaths;
              var normalizedTests = component.tests ? component.tests.map(function (testFile) {
                return (_path || _load_path()).default.normalize(testFile.trim());
              }) : [];
              component.tests = normalizedTests;
              component.exclude = component.exclude ? component.exclude.map(function (excludeFile) {
                return (_path || _load_path()).default.normalize(excludeFile.trim());
              }) : [];
              var addComponents = new (_addComponents || _load_addComponents()).default(addContext, component);
              addComponentsArr.push(addComponents);
            });
            addResults = [];
            _context3.next = 10;
            return Promise.all(addComponentsArr.map(function () {
              var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(addComponents) {
                var addResultsSingle;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
                  while (1) {
                    switch (_context2.prev = _context2.next) {
                      case 0:
                        _context2.next = 2;
                        return addComponents.add();

                      case 2:
                        addResultsSingle = _context2.sent;

                        addResults.push(addResultsSingle);

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

              return function (_x4) {
                return _ref3.apply(this, arguments);
              };
            }()));

          case 10:
            _context3.next = 12;
            return consumer.onDestroy();

          case 12:
            return _context3.abrupt('return', addResults);

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

  return function addMany(_x2, _x3) {
    return _ref2.apply(this, arguments);
  };
}();

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _addComponents;

function _load_addComponents() {
  return _addComponents = _interopRequireDefault(require('../../../consumer/component-ops/add-components'));
}

var _consumer;

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

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