/**
 * Twilio Voice Channel Adapter
 *
 * Manages voice calls via Twilio API.
 * This is a stub implementation.
 */
import { logger } from '../../utils/logger.js';
let callCounter = 0;
export class TwilioVoiceAdapter {
    config;
    running = false;
    activeCalls = new Map();
    constructor(config) {
        this.config = { ...config };
    }
    async start() {
        if (this.running) {
            throw new Error('TwilioVoiceAdapter is already running');
        }
        logger.debug('TwilioVoiceAdapter: initializing', {
            accountSid: this.config.accountSid,
            phoneNumber: this.config.phoneNumber,
        });
        this.running = true;
    }
    async stop() {
        if (!this.running) {
            throw new Error('TwilioVoiceAdapter is not running');
        }
        logger.debug('TwilioVoiceAdapter: stopping');
        this.activeCalls.clear();
        this.running = false;
    }
    isRunning() {
        return this.running;
    }
    async makeCall(to, message) {
        if (!this.running) {
            throw new Error('TwilioVoiceAdapter is not running');
        }
        const callSid = `CA${Date.now()}_${++callCounter}`;
        this.activeCalls.set(callSid, { to, startedAt: new Date() });
        logger.debug('TwilioVoiceAdapter: making call', { to, callSid });
        return { success: true, callSid };
    }
    async endCall(callSid) {
        if (!this.running) {
            throw new Error('TwilioVoiceAdapter is not running');
        }
        const existed = this.activeCalls.delete(callSid);
        logger.debug('TwilioVoiceAdapter: ending call', { callSid, existed });
        return { success: existed };
    }
    getActiveCalls() {
        return Array.from(this.activeCalls.entries()).map(([callSid, info]) => ({
            callSid,
            ...info,
        }));
    }
    generateTwiML(message) {
        return `<?xml version="1.0" encoding="UTF-8"?><Response><Say>${this.escapeXml(message)}</Say></Response>`;
    }
    getConfig() {
        return { ...this.config };
    }
    escapeXml(str) {
        return str
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&apos;');
    }
}
export default TwilioVoiceAdapter;
//# sourceMappingURL=index.js.map