"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.XmppListenerManager = void 0;
const activeListeners = new Map();
class XmppListenerManager {
  static addListener(client, eventType, listenerKey, handler) {
    this.removeListener(listenerKey);
    activeListeners.set(listenerKey, {
      handler,
      client,
      eventType,
      timestamp: Date.now()
    });
    client.on(eventType, handler);
    if (process.env.NODE_ENV === 'development') {
      console.log(`[XmppListenerManager] Added listener: ${listenerKey}, Total: ${activeListeners.size}`);
    }
  }
  static removeListener(listenerKey) {
    const listener = activeListeners.get(listenerKey);
    if (listener) {
      listener.client.off(listener.eventType, listener.handler);
      activeListeners.delete(listenerKey);
      if (process.env.NODE_ENV === 'development') {
        console.log(`[XmppListenerManager] Removed listener: ${listenerKey}, Total: ${activeListeners.size}`);
      }
      return true;
    }
    return false;
  }
  static removeAllListenersForClient(client) {
    let removedCount = 0;
    for (const [key, listener] of activeListeners.entries()) {
      if (listener.client === client) {
        listener.client.off(listener.eventType, listener.handler);
        activeListeners.delete(key);
        removedCount++;
      }
    }
    if (process.env.NODE_ENV === 'development' && removedCount > 0) {
      console.log(`[XmppListenerManager] Removed ${removedCount} listeners for client, Total: ${activeListeners.size}`);
    }
  }
  static getListenerKey(operation, identifier) {
    return `${operation}-${identifier}`;
  }
  static clearAllListeners() {
    const count = activeListeners.size;
    for (const [key, listener] of activeListeners.entries()) {
      listener.client.off(listener.eventType, listener.handler);
    }
    activeListeners.clear();
    if (process.env.NODE_ENV === 'development') {
      console.log(`[XmppListenerManager] Cleared all ${count} listeners`);
    }
  }
  static getActiveListenerCount() {
    return activeListeners.size;
  }
  static getActiveListenersInfo() {
    const now = Date.now();
    return Array.from(activeListeners.entries()).map(([key, listener]) => ({
      key,
      eventType: listener.eventType,
      timestamp: listener.timestamp,
      age: now - listener.timestamp
    }));
  }
  static logActiveListeners() {
    if (process.env.NODE_ENV === 'development') {
      const info = this.getActiveListenersInfo();
      console.log(`[XmppListenerManager] Active listeners (${info.length}):`, info);
    }
  }
}
exports.XmppListenerManager = XmppListenerManager;
//# sourceMappingURL=listenerManager.js.map