"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const body_parser_1 = __importDefault(require("body-parser"));
const express_1 = __importDefault(require("express"));
const processPayload_1 = __importDefault(require("./processPayload"));
class Server {
    constructor(port, verifyToken) {
        this.handleVerification = (req, res) => {
            // console.log("Request Data: ", req.query)
            // Handle Verification
            if (req.query["hub.verify_token"]) {
                let hub = req.query;
                console.log("Hub: ", hub);
                let verify_token = req.query["hub.verify_token"];
                if (verify_token == process.env.VERIFY_TOKEN) {
                    return res.send(req.query["hub.challenge"]);
                }
                return res.send("Invalid verification token");
            }
            return res.send("This is a default response");
        };
        this.port = port;
        this.verifyToken = verifyToken;
    }
    start(notificationCallback) {
        const app = (0, express_1.default)();
        // Middleware 
        app.use(body_parser_1.default.urlencoded({ extended: false }));
        app.use(body_parser_1.default.json());
        app.get('/', this.handleVerification);
        app.post("/", (req, res) => __awaiter(this, void 0, void 0, function* () {
            let data = req.body;
            let ppayload = new processPayload_1.default(data);
            let resp = yield notificationCallback(data, ppayload);
            return res.json(resp);
        }));
        app.listen(this.port, () => {
            // console.log(`Example app listening on port ${this.port}`)
        });
        return app;
    }
}
exports.default = Server;
