'use strict';

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

var _child_process;

function _load_child_process() {
  return _child_process = require('child_process');
}

var _path;

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

var _component;

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

var _globalConfig;

function _load_globalConfig() {
  return _globalConfig = _interopRequireWildcard(require('../../api/consumer/lib/global-config'));
}

var _constants;

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function defaultCIFunc(id, scopePath) {
  var child = (0, (_child_process || _load_child_process()).spawn)(process.argv[0], [(_path || _load_path()).default.join(__dirname, 'ci-worker.js')], {
    detached: true,
    cwd: scopePath,
    env: {
      __id__: id,
      __scope__: scopePath
    },
    stdio: ['ignore', 'ignore', 'ignore']
  });

  child.unref();
}

exports.default = function (component, scopePath) {
  var enableCI = (_globalConfig || _load_globalConfig()).getSync((_constants || _load_constants()).CFG_CI_ENABLE_KEY);
  var ciFuncPath = (_globalConfig || _load_globalConfig()).getSync((_constants || _load_constants()).CFG_CI_FUNCTION_PATH_KEY);

  if (enableCI !== 'true' && !ciFuncPath) return component;

  var id = component.id.toString();
  var ciFunc = void 0;

  if (!ciFuncPath) ciFunc = defaultCIFunc;else {
    // $FlowFixMe
    ciFunc = require(ciFuncPath);
  }

  ciFunc(id, scopePath);
  return component;
};