/**
 * Response Waiter
 *
 * Waits for user responses with timeout, collecting from multiple channels.
 */
import { EventEmitter } from 'events';
export class ResponseWaiter extends EventEmitter {
    pending = new Map();
    timers = new Map();
    /**
     * Wait for a response from any channel
     */
    async waitForResponse(id, question, channels, timeoutMs = 60000) {
        return new Promise((resolve) => {
            const pending = {
                id,
                question,
                channels,
                timeoutMs,
                createdAt: new Date(),
                resolve,
            };
            this.pending.set(id, pending);
            const timer = setTimeout(() => {
                this.pending.delete(id);
                this.timers.delete(id);
                this.emit('timeout', { id, question });
                resolve(null);
            }, timeoutMs);
            this.timers.set(id, timer);
            this.emit('waiting', { id, question, channels });
        });
    }
    /**
     * Deliver a response
     */
    deliverResponse(id, response, channel) {
        const pending = this.pending.get(id);
        if (!pending)
            return false;
        // Clear timer
        const timer = this.timers.get(id);
        if (timer)
            clearTimeout(timer);
        this.pending.delete(id);
        this.timers.delete(id);
        pending.resolve(response);
        this.emit('response', { id, response, channel });
        return true;
    }
    /**
     * Cancel a pending wait
     */
    cancel(id) {
        const pending = this.pending.get(id);
        if (!pending)
            return false;
        const timer = this.timers.get(id);
        if (timer)
            clearTimeout(timer);
        this.pending.delete(id);
        this.timers.delete(id);
        pending.resolve(null);
        this.emit('cancelled', { id });
        return true;
    }
    /**
     * Get pending count
     */
    getPendingCount() {
        return this.pending.size;
    }
    /**
     * List pending responses
     */
    listPending() {
        return Array.from(this.pending.values()).map(p => ({
            id: p.id,
            question: p.question,
            channels: p.channels,
            age: Date.now() - p.createdAt.getTime(),
        }));
    }
    /**
     * Cancel all pending
     */
    cancelAll() {
        for (const [id] of this.pending) {
            this.cancel(id);
        }
    }
}
//# sourceMappingURL=response-waiter.js.map