import { EventEmitter } from 'events';
export class TypingIndicatorManager extends EventEmitter {
    sessions = new Map();
    presence = { status: 'online', lastActivity: Date.now() };
    intervalMs;
    constructor(intervalMs = 4000) {
        super();
        this.intervalMs = intervalMs;
    }
    startTyping(channel, chatId) {
        const key = `${channel}:${chatId}`;
        if (this.sessions.has(key))
            return key;
        const session = {
            channel,
            chatId,
            interval: null,
            active: true,
            startedAt: Date.now(),
        };
        this.emit('typing', { channel, chatId, typing: true });
        session.interval = setInterval(() => {
            if (session.active) {
                this.emit('typing', { channel, chatId, typing: true });
            }
        }, this.intervalMs);
        this.sessions.set(key, session);
        this.updatePresence('busy');
        return key;
    }
    stopTyping(key) {
        const session = this.sessions.get(key);
        if (!session)
            return;
        session.active = false;
        if (session.interval) {
            clearInterval(session.interval);
            session.interval = null;
        }
        this.emit('typing', { channel: session.channel, chatId: session.chatId, typing: false });
        this.sessions.delete(key);
        if (this.sessions.size === 0) {
            this.updatePresence('online');
        }
    }
    stopAll() {
        const keys = Array.from(this.sessions.keys());
        for (const key of keys) {
            this.stopTyping(key);
        }
    }
    updatePresence(status, task) {
        this.presence = {
            status,
            lastActivity: Date.now(),
            currentTask: task,
        };
        this.emit('presence', { ...this.presence });
    }
    getPresence() {
        return { ...this.presence };
    }
    getActiveCount() {
        return this.sessions.size;
    }
    dispose() {
        this.stopAll();
        this.removeAllListeners();
    }
}
//# sourceMappingURL=typing-indicator.js.map