'use strict';

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

var _regenerator;

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

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var makeEnvFromModel = exports.makeEnvFromModel = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(envType, modelObject, repository) {
    var actualObject, envExtensionProps, extension;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            (_logger || _load_logger()).default.debug(`env-factory, create ${envType} from model`);

            if (modelObject) {
              _context2.next = 3;
              break;
            }

            return _context2.abrupt('return', undefined);

          case 3:
            actualObject = typeof modelObject === 'string' ? _extends({ envType }, (_baseExtension || _load_baseExtension()).default.transformStringToModelObject(modelObject)) : _extends({ envType }, modelObject);
            _context2.next = 6;
            return (_envExtension || _load_envExtension()).default.loadFromModelObject(actualObject, repository);

          case 6:
            envExtensionProps = _context2.sent;
            extension = getEnvInstance(envType, envExtensionProps);
            return _context2.abrupt('return', extension);

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

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

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../logger/logger'));
}

var _testerExtension;

function _load_testerExtension() {
  return _testerExtension = _interopRequireDefault(require('./tester-extension'));
}

var _compilerExtension;

function _load_compilerExtension() {
  return _compilerExtension = _interopRequireDefault(require('./compiler-extension'));
}

var _envExtension;

function _load_envExtension() {
  return _envExtension = _interopRequireDefault(require('./env-extension'));
}

var _baseExtension;

function _load_baseExtension() {
  return _baseExtension = _interopRequireDefault(require('./base-extension'));
}

var _repository;

function _load_repository() {
  return _repository = _interopRequireDefault(require('../scope/objects/repository'));
}

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(envType, props) {
    var envExtensionProps, extension, throws;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            (_logger || _load_logger()).default.debug(`env-factory, create ${envType}`);
            props.envType = envType;
            props.throws = true;
            _context.next = 5;
            return (_envExtension || _load_envExtension()).default.load(props);

          case 5:
            envExtensionProps = _context.sent;
            extension = getEnvInstance(envType, envExtensionProps);

            if (!extension.loaded) {
              _context.next = 11;
              break;
            }

            throws = true;
            _context.next = 11;
            return extension.init(throws);

          case 11:
            return _context.abrupt('return', extension);

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

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

  return makeEnv;
}();

function getEnvInstance(envType, envExtensionProps) {
  switch (envType) {
    case (_constants || _load_constants()).COMPILER_ENV_TYPE:
      return new (_compilerExtension || _load_compilerExtension()).default(envExtensionProps);
    case (_constants || _load_constants()).TESTER_ENV_TYPE:
      return new (_testerExtension || _load_testerExtension()).default(envExtensionProps);
    default:
      throw new Error(`unrecognized env type ${envType}`);
  }
}