'use strict';

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

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _handlebars = require('handlebars');

var _handlebars2 = _interopRequireDefault(_handlebars);

var _hooks = require('../../hooks/hooks');

var _hooks2 = _interopRequireDefault(_hooks);

var _cli = require('../../cli');

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

var route = function route(req, res, next) {
  var html = '';

  _cli.abeExtend.hooks.instance.trigger('beforeRoute', req, res, next);

  var page = _path2.default.join(__dirname + '/../views/list-hooks.html');
  if (_cli.coreUtils.file.exist(page)) {
    html = _fsExtra2.default.readFileSync(page, 'utf8');
  }

  var allHooks = [];

  Array.prototype.forEach.call(Object.keys(_hooks2.default), function (hook) {
    var hookString = _hooks2.default[hook] + '';
    var match = /\((.*?)\)/.exec(hookString);
    var matchReturn = /return ([a-z1-Z-1-9]+)/.exec(hookString);
    allHooks.push({
      name: hook,
      params: match ? match[1] : 'null',
      back: matchReturn ? matchReturn[1].replace(';', '') : 'null'
    });
  });

  var template = _handlebars2.default.compile(html, { noEscape: true });
  var tmp = template({
    hooks: allHooks
  });

  return res.send(tmp);
};

exports.default = route;