'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _express = require('express');

var _express2 = _interopRequireDefault(_express);

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

var abe = _interopRequireWildcard(_cli);

var _routes = require('../routes');

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 }; }

var router = _express2.default.Router();
_cli.abeExtend.hooks.instance.trigger('afterHandlebarsHelpers', _cli.Handlebars);
_cli.abeExtend.hooks.instance.trigger('beforeAddRoute', router);

router.get('/abe/users/forgot', _routes.users.getForgot);
router.get('/abe/users/list', _routes.users.getList);
router.get('/abe/users/login', _routes.users.getLogin);
router.get('/abe/users/logout', _routes.users.getLogout);
router.get('/abe/users/reset', _routes.users.getReset);
router.post('/abe/users/activate', _routes.users.postActivate);
router.post('/abe/users/add', _routes.users.postAdd);
router.post('/abe/users/deactivate', _routes.users.postDeactivate);
router.post('/abe/users/login', _routes.users.postLogin);
router.post('/abe/users/remove', _routes.users.postRemove);
router.post('/abe/users/reset', _routes.users.postReset);
router.post('/abe/users/update', _routes.users.postUpdate);

router.get('/abe/paginate', _routes.getPaginate);
router.post('/abe/create*', _routes.postCreate);
router.post('/abe/duplicate*', _routes.postDuplicate);
router.post('/abe/update*', _routes.postUpdate);
router.post('/abe/sql-request*', _routes.postSqlRequest);
router.post('/abe/page/*', _routes.postPage);
router.get('/abe/page/*', _routes.getPage);
router.get('/abe/generate-posts', _routes.getGeneratePost);
router.get('/abe/save-config', _routes.getSaveConfig);
router.get('/abe/reference', _routes.getReference);
router.get('/abe/structure', _routes.getStructure);
router.get('/abe/thumbs/*', _routes.getThumbs);
router.get('/abe/image/*', _routes.getImage);
router.post('/abe/upload/*', _routes.postUpload);
router.post('/abe/reference/*', _routes.postReference);
router.post('/abe/structure/*', _routes.postStructure);
router.get('/abe/editor*', _routes.getMain);
router.post('/abe/list-url/save*', _routes.postListUrlSave);

router.get('/abe/list-workflow*', function (req, res, next) {
  (0, _routes.getListWorkflow)(router, req, res, next);
});
router.get('/abe/list-url*', function (req, res, next) {
  (0, _routes.getListUrl)(router, req, res, next);
});
router.get('/abe/list-hooks*', _routes.getListHooks);

var workflows = _cli.config.users.workflow;
Array.prototype.forEach.call(workflows, function (workflow) {
  router.get('/abe/operations/delete/' + workflow + '*', _routes.operations.getDelete);

  if (workflow != 'draft' && workflow != 'publish') {
    router.post('/abe/operations/reject/' + workflow + '*', _routes.operations.postReject);
  } else if (workflow == 'publish') {
    router.get('/abe/operations/unpublish*', _routes.operations.getUnpublish);
  }

  router.post('/abe/operations/submit/' + workflow + '*', _routes.operations.postSubmit);
  router.post('/abe/operations/edit/' + workflow + '*', _routes.operations.postEdit);
});

var routes = _cli.abeExtend.plugins.instance.getRoutes();
Array.prototype.forEach.call(routes, function (route) {
  if (typeof route.get !== 'undefined' && route.get !== null) {
    Array.prototype.forEach.call(route.get, function (routeGet) {
      try {
        var pluginRoot = require(routeGet.path);
        if (_typeof(pluginRoot.default) === 'object') {
          router.get(routeGet.routePath, pluginRoot.default);
        } else {
          router.get(routeGet.routePath, function (req, res, next) {
            pluginRoot.default(req, res, next, abe);
          });
        }
      } catch (e) {
        // statements
        console.log(e);
      }
    });
  }
  if (typeof route.post !== 'undefined' && route.post !== null) {
    Array.prototype.forEach.call(route.post, function (routePost) {
      try {
        var pluginRoot = require(routePost.path);
        if (_typeof(pluginRoot.default) === 'object') {
          router.post(routePost.routePath, pluginRoot.default);
        } else {
          router.post(routePost.routePath, function (req, res, next) {
            pluginRoot.default(req, res, next, abe);
          });
        }
      } catch (e) {
        // statements
        console.log(e);
      }
    });
  }
});

router.get('/abe*', _routes.getHome);

_cli.abeExtend.hooks.instance.trigger('afterAddRoute', router);

exports.default = router;