"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const HttpParser_1 = require("../parser/HttpParser");
/**
 * Defines and registers global contoller which will handle any request not handled by admin/management endpoints
 */
class GlobalController {
    constructor(app, mocksDir) {
        /**
         * Define a generic route for all requests
         * Following holds true for all the generic routes i.e. GET, POST, DELETE, PUT
         * We create a parser object by initializing our Parser class with request, response and mocksDir objects
         * The parser object in turn will give us access to the path of a matched directory for an incoming request.
         * Depending on the HTTP Method of the incoming request we append /GET.mock, /POST.mock, /DELETE.mock or /PUT.mock to the matched directory
         * Parse object also gives us access to a method getResponse which does the following tasks:
         *   - Read the response from the specified file
         *   - Run all the handlebars compilations as required.
         *   - Generate a HTTP Response
         *   - Send the response to client.
         * @returns {void}
         */
        this.register = () => {
            this.app.get("*", (req, res) => {
                this.handler(req, res, "GET");
            });
            this.app.post("*", (req, res) => {
                this.handler(req, res, "POST");
            });
            this.app.put("*", (req, res) => {
                this.handler(req, res, "PUT");
            });
            this.app.delete("*", (req, res) => {
                this.handler(req, res, "DELETE");
            });
            this.app.head("*", (req, res) => {
                this.handler(req, res, "HEAD");
            });
            this.app.connect("*", (req, res) => {
                this.handler(req, res, "CONNECT");
            });
            this.app.options("*", (req, res) => {
                this.handler(req, res, "OPTIONS");
            });
            this.app.trace("*", (req, res) => {
                this.handler(req, res, "TRACE");
            });
            this.app.patch("*", (req, res) => {
                this.handler(req, res, "PATCH");
            });
        };
        this.handler = (req, res, verb) => {
            const parser = new HttpParser_1.HttpParser(req, res, this.mocksDir);
            const mockFile = parser.getMatchedDir() + `/${verb}.mock`;
            parser.getResponse(mockFile);
        };
        this.app = app;
        this.mocksDir = mocksDir;
        this.register();
    }
}
exports.default = GlobalController;
//# sourceMappingURL=GlobalController.js.map