'use strict';

var _requestify;

function _load_requestify() {
  return _requestify = _interopRequireDefault(require('requestify'));
}

var _globalConfig;

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

var _constants;

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

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

var ANALYTICS_DOMAIN = (0, (_globalConfig || _load_globalConfig()).getSync)((_constants || _load_constants()).CFG_ANALYTICS_DOMAIN_KEY) || (_constants || _load_constants()).DEFAULT_ANALYTICS_DOMAIN;
/**
 * to debug errors here, first, change the parent to have { silent: false }, in analytics.js `fork` call.
 */
/* eslint no-console: 0 */
process.on('message', function (msg) {
  // needed for the parent to make sure the child got the message
  // without it, when the message is large, the parent exits before it totally sent
  process.send('Got the message');

  return (_requestify || _load_requestify()).default.post(ANALYTICS_DOMAIN, msg, { timeout: 1000 }).then(function () {
    console.log('message has been sent');
    process.exit();
  }).fail(function (err) {
    console.error('failed to send the message', err);
    process.exit();
  });
});

setTimeout(function () {
  console.log('exit due to timeout');
  process.exit();
}, 5000);