"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.makeEnvFromModel = makeEnvFromModel;
exports.default = void 0;

function _objectSpread2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

  _objectSpread2 = function () {
    return data;
  };

  return data;
}

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

  _bluebird = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

function _testerExtension() {
  const data = _interopRequireDefault(require("./tester-extension"));

  _testerExtension = function () {
    return data;
  };

  return data;
}

function _compilerExtension() {
  const data = _interopRequireDefault(require("./compiler-extension"));

  _compilerExtension = function () {
    return data;
  };

  return data;
}

function _envExtension() {
  const data = _interopRequireDefault(require("./env-extension"));

  _envExtension = function () {
    return data;
  };

  return data;
}

function _baseExtension() {
  const data = _interopRequireDefault(require("./base-extension"));

  _baseExtension = function () {
    return data;
  };

  return data;
}

function _repository() {
  const data = _interopRequireDefault(require("../scope/objects/repository"));

  _repository = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _makeEnv = (0, _bluebird().coroutine)(function* (envType, props) {
    _logger().default.debug(`env-factory, create ${envType}`);

    props.envType = envType;
    props.throws = true;
    const envExtensionProps = yield _envExtension().default.load(props);
    const extension = getEnvInstance(envType, envExtensionProps);

    if (extension.loaded) {
      const throws = true;
      yield extension.init(throws);
    }

    return extension;
  });

  function makeEnv(_x, _x2) {
    return _makeEnv.apply(this, arguments);
  }

  return makeEnv;
}();

exports.default = _default;

function makeEnvFromModel(_x3, _x4, _x5) {
  return _makeEnvFromModel.apply(this, arguments);
}

function _makeEnvFromModel() {
  _makeEnvFromModel = (0, _bluebird().coroutine)(function* (envType, modelObject, repository) {
    _logger().default.debug(`env-factory, create ${envType} from model`);

    if (!modelObject) return undefined;
    const actualObject = typeof modelObject === 'string' ? (0, _objectSpread2().default)({
      envType
    }, _baseExtension().default.transformStringToModelObject(modelObject)) : (0, _objectSpread2().default)({
      envType
    }, modelObject);
    const envExtensionProps = yield _envExtension().default.loadFromModelObject(actualObject, repository);
    const extension = getEnvInstance(envType, envExtensionProps);
    return extension;
  });
  return _makeEnvFromModel.apply(this, arguments);
}

function getEnvInstance(envType, envExtensionProps) {
  switch (envType) {
    case _constants().COMPILER_ENV_TYPE:
      return new (_compilerExtension().default)(envExtensionProps);

    case _constants().TESTER_ENV_TYPE:
      return new (_testerExtension().default)(envExtensionProps);

    default:
      throw new Error(`unrecognized env type ${envType}`);
  }
}