var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import JSONWebToken from "../Utils/JsonWebToken";
import Response from "../Utils/Response";
import logger, { getLogAttributesFromRequest } from "../Utils/Logger";
import BadDataException from "../../Types/Exception/BadDataException";
import JSONFunctions from "../../Types/JSONFunctions";
import VoiceResponse from "twilio/lib/twiml/VoiceResponse";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export default class NotificationMiddleware {
    static async sendResponse(req, res, onCallInputRequest) {
        const response = new VoiceResponse();
        if (onCallInputRequest[req.body["Digits"]]) {
            response.say(onCallInputRequest[req.body["Digits"]].sayMessage);
        }
        else {
            response.say(onCallInputRequest["default"].sayMessage);
        }
        return Response.sendXmlResponse(req, res, response.toString());
    }
    static async isValidCallNotificationRequest(req, res, next) {
        req = req;
        if (!req.body["Digits"]) {
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid input"));
        }
        if (!req.query["token"]) {
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid token"));
        }
        const token = req.query["token"];
        try {
            req.callTokenData = JSONFunctions.deserialize(JSONWebToken.decodeJsonPayload(token));
        }
        catch (e) {
            logger.error(e, getLogAttributesFromRequest(req));
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid token"));
        }
        return next();
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object]),
    __metadata("design:returntype", Promise)
], NotificationMiddleware, "sendResponse", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Function]),
    __metadata("design:returntype", Promise)
], NotificationMiddleware, "isValidCallNotificationRequest", null);
//# sourceMappingURL=NotificationMiddleware.js.map