'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.dateStart = exports.processConfig = exports.error = exports.trace = exports.log = undefined;
exports.getTime = getTime;
exports.init = init;

var _debug = require('debug');

var _debug2 = _interopRequireDefault(_debug);

var _extend = require('extend');

var _extend2 = _interopRequireDefault(_extend);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

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

var Manager = require('../../cli').Manager;
var Handlebars = require('../../cli').Handlebars;
var config = require('../../cli').config;
var abeExtend = require('../../cli').abeExtend;

var log = exports.log = undefined;
var trace = exports.trace = undefined;
var error = exports.error = undefined;
var processConfig = exports.processConfig = undefined;
var dateStart = exports.dateStart = undefined;

function getTime() {
  return (0, _moment2.default)((0, _moment2.default)() - dateStart).format('mm:ss') + 'sec';
}

function init(processName, conf) {
  var p = new Promise(function (resolve) {

    exports.log = log = (0, _debug2.default)(processName + ':log');
    log.color = 2;
    exports.trace = trace = (0, _debug2.default)(processName + ':trace');
    exports.error = error = (0, _debug2.default)(processName + ':error');
    error.color = 1;

    exports.processConfig = processConfig = {};
    Array.prototype.forEach.call(process.argv, function (item) {
      if (item.indexOf('=') > -1) {
        var ar = item.split('=');
        processConfig[ar[0]] = ar[1];
      }
    });
    if (processConfig.ABE_WEBSITE) {
      config.set({ root: processConfig.ABE_WEBSITE.replace(/\/$/, '') + '/' });
    }

    exports.processConfig = processConfig = (0, _extend2.default)(true, conf, processConfig);

    if (typeof processConfig.ABE_WEBSITE !== 'undefined' && processConfig.ABE_WEBSITE !== null) {
      abeExtend.hooks.instance.trigger('afterHandlebarsHelpers', Handlebars);

      Manager.instance.init().then(function () {
        exports.dateStart = dateStart = (0, _moment2.default)();
        resolve();
      }).catch(function (e) {
        error(processName + ' ' + e);
      });
    } else {
      error('ABE_WEBSITE is not defined use node process.js ABE_WEBSITE=/pat/to/website');
      process.exit(0);
    }
  });

  return p;
}