"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _package = _interopRequireDefault(require("../../../../../package.json"));

var errorHandler = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(app, config) {
    var path, fs, io;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            path = require("path");
            fs = require("fs");
            io = app.io;
            _context2.next = 5;
            return app.setNotFoundHandler( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(request, response) {
                var mHeader, html;
                return _regenerator["default"].wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        mHeader = {
                          FrameworkName: _package["default"].name,
                          FrameworkVersion: _package["default"].version,
                          FrameworkAuthor: _package["default"].author
                        };
                        _context.next = 3;
                        return Object.keys(mHeader).forEach(function (keys) {
                          response.header(keys, mHeader[keys]);
                        });

                      case 3:
                        _context.next = 5;
                        return Object.keys(mHeader).forEach(function (keys) {
                          request.headers[keys] = mHeader[keys];
                        });

                      case 5:
                        _context.next = 7;
                        return fs.readFileSync(path.join(__dirname, "./notFound.html"), {
                          encoding: 'utf8',
                          flag: 'r'
                        });

                      case 7:
                        html = _context.sent;
                        _context.t0 = request.method;
                        _context.next = _context.t0 === "GET" ? 11 : 14;
                        break;

                      case 11:
                        _context.next = 13;
                        return response.code(404).type('text/html').send(html);

                      case 13:
                        return _context.abrupt("break", 16);

                      case 14:
                        _context.next = 16;
                        return response.code(404).send({
                          status: false,
                          code: 404,
                          msg: "Page Not Found"
                        });

                      case 16:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              }));

              return function (_x3, _x4) {
                return _ref2.apply(this, arguments);
              };
            }());

          case 5:
            return _context2.abrupt("return", app);

          case 6:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function errorHandler(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

var _default = errorHandler;
exports["default"] = _default;
//# sourceMappingURL=errorHandler.js.map