"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleOutboundAnsweredController = void 0;
const twilio_1 = __importDefault(require("twilio"));
const logger_1 = __importDefault(require("../../../logger"));
/**
 * Controller for handling answered simple outbound calls
 * This connects directly to media stream so AI can speak first
 */
const simpleOutboundAnsweredController = async (req, res) => {
    try {
        const twiml = new twilio_1.default.twiml.VoiceResponse();
        const host = req.headers.host;
        // Add 5-second pause before connecting to media stream for outbound calls
        // This gives the called party time to answer and prepare
        twiml.pause({ length: 5 });
        const protocol = req.secure || req.headers['x-forwarded-proto'] === 'https' ? 'wss' : 'ws';
        const streamUrl = `${protocol}://${host}/api/phone/media-stream?outbound=true`;
        const connect = twiml.connect();
        connect.stream({ url: streamUrl });
        res.type('text/xml');
        res.send(twiml.toString());
    }
    catch (error) {
        logger_1.default.error(`Error in simple outbound answered webhook: ${error.message}`);
        const twiml = new twilio_1.default.twiml.VoiceResponse();
        twiml.say('An application error has occurred. Goodbye.');
        twiml.hangup();
        res.type('text/xml');
        res.send(twiml.toString());
    }
};
exports.simpleOutboundAnsweredController = simpleOutboundAnsweredController;
//# sourceMappingURL=simple-outbound-answered.js.map