"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _addComponents() {
  const data = _interopRequireDefault(require("../../../consumer/component-ops/add-components"));

  _addComponents = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _hooks() {
  const data = _interopRequireDefault(require("../../../hooks"));

  _hooks = function () {
    return data;
  };

  return data;
}

const HooksManagerInstance = _hooks().default.getInstance();

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

function _addOne() {
  _addOne = (0, _bluebird().coroutine)(function* (addProps) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const addContext = {
      consumer
    };
    const addComponents = new (_addComponents().default)(addContext, addProps);
    const addResults = yield addComponents.add();
    yield consumer.onDestroy();
    const hookContext = {
      workspacePath: consumer.getPath(),
      bitmapFileName: _constants().BIT_MAP
    };
    yield HooksManagerInstance.triggerHook(_constants().POST_ADD_HOOK, addResults, null, hookContext);
    return addResults;
  });
  return _addOne.apply(this, arguments);
}

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

function _addMany() {
  _addMany = (0, _bluebird().coroutine)(function* (components, alternateCwd) {
    // 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()
    const consumerPath = alternateCwd || process.cwd();
    const consumer = yield (0, _consumer().loadConsumer)(consumerPath);
    const addContext = {
      consumer,
      alternateCwd: consumerPath
    };
    const addComponentsArr = [];
    components.forEach(component => {
      const normalizedPaths = component.componentPaths.map(p => {
        return _path().default.normalize(p);
      });
      component.componentPaths = normalizedPaths;
      const normalizedTests = component.tests ? component.tests.map(testFile => _path().default.normalize(testFile.trim())) : [];
      component.tests = normalizedTests;
      component.exclude = component.exclude ? component.exclude.map(excludeFile => _path().default.normalize(excludeFile.trim())) : [];
      const addComponents = new (_addComponents().default)(addContext, component);
      addComponentsArr.push(addComponents);
    });
    const addResults = [];
    yield Promise.all(addComponentsArr.map(
    /*#__PURE__*/
    function () {
      var _ref = (0, _bluebird().coroutine)(function* (addComponents) {
        const addResultsSingle = yield addComponents.add();
        addResults.push(addResultsSingle);
      });

      return function (_x4) {
        return _ref.apply(this, arguments);
      };
    }()));
    yield consumer.onDestroy();
    const hookContext = {
      workspacePath: consumer.getPath(),
      bitmapFileName: _constants().BIT_MAP
    };
    yield HooksManagerInstance.triggerHook(_constants().POST_ADD_HOOK, addResults, null, hookContext);
    return addResults;
  });
  return _addMany.apply(this, arguments);
}