"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.BaseEventManager = void 0;

const event_validator_1 = require("./event-validator"), event_performance_1 = require("./event-performance");

class BaseEventManager {
    constructor(eventBus, config = {}) {
        this.callbackMap = new WeakMap, this.eventBus = eventBus, this.config = Object.assign({
            enableValidation: !0,
            enablePerformanceMonitoring: !1,
            enableErrorBoundary: !0,
            maxListeners: 100
        }, config), this.performanceOptimizer = new event_performance_1.EventPerformanceOptimizer;
    }
    on(type, callback) {
        let finalCallback = callback;
        this.config.enableValidation && (finalCallback = event => {
            this.validateEvent(type, event) && callback(event);
        }), this.eventBus.on(type, finalCallback), finalCallback !== callback && this.callbackMap.set(callback, finalCallback);
    }
    off(type, callback) {
        if (callback) {
            const optimizedCallback = this.callbackMap.get(callback);
            optimizedCallback ? (this.eventBus.off(type, optimizedCallback), this.callbackMap.delete(callback)) : this.eventBus.off(type, callback);
        } else this.eventBus.off(type), this.callbackMap = new WeakMap;
    }
    emit(type, event) {
        this.config.enableValidation && !this.validateEvent(type, event) || this.eventBus.emit(type, event);
    }
    getListenerCount(type) {
        if (type) return this.eventBus.listenerCount(type);
        return this.getEventTypes().reduce(((total, eventType) => total + this.eventBus.listenerCount(eventType)), 0);
    }
    clearAllListeners() {
        this.getEventTypes().forEach((eventType => {
            this.eventBus.removeAllListeners(eventType);
        })), this.performanceOptimizer.clearAll(), this.callbackMap = new WeakMap;
    }
    getStatistics() {
        const eventTypes = this.getEventTypes(), listenersByType = {};
        return eventTypes.forEach((type => {
            listenersByType[type] = this.eventBus.listenerCount(type);
        })), {
            totalEvents: eventTypes.length,
            listenersByType: listenersByType,
            totalListeners: Object.values(listenersByType).reduce(((sum, count) => sum + count), 0)
        };
    }
    validateEvent(eventType, event) {
        return event_validator_1.EventValidator.validate(eventType, event);
    }
}

exports.BaseEventManager = BaseEventManager;
//# sourceMappingURL=base-event-manager.js.map