"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventSubscriberRegistry = void 0;
const common_1 = require("@nestjs/common");
let EventSubscriberRegistry = class EventSubscriberRegistry {
    constructor() {
        this.subscribers = new Map();
    }
    /**
     * Register a subscriber for an event
     */
    registerHandler(definition) {
        const { eventName } = definition;
        if (!this.subscribers.has(eventName)) {
            this.subscribers.set(eventName, []);
        }
        this.subscribers.get(eventName).push(definition);
        // Sort by priority (higher priority first)
        const handlers = this.subscribers.get(eventName);
        handlers.sort((a, b) => (b.priority || 0) - (a.priority || 0));
    }
    /**
     * Ergonomic registration for module services.
     */
    subscribe(eventName, consumerName, handler, priority = 0) {
        this.registerHandler({
            eventName,
            consumerName,
            handler,
            priority,
        });
    }
    /**
     * Register multiple handlers in one call.
     */
    subscribeMany(definitions) {
        for (const definition of definitions) {
            this.registerHandler(definition);
        }
    }
    /**
     * Get all handlers for an event, sorted by priority
     */
    getHandlers(eventName) {
        return this.subscribers.get(eventName) || [];
    }
    /**
     * Check if event has any handlers
     */
    hasHandlers(eventName) {
        return this.subscribers.has(eventName) && this.subscribers.get(eventName).length > 0;
    }
    /**
     * Get all registered event names
     */
    getEventNames() {
        return Array.from(this.subscribers.keys());
    }
    /**
     * Get total count of all registered handlers
     */
    getHandlerCount() {
        let count = 0;
        for (const handlers of this.subscribers.values()) {
            count += handlers.length;
        }
        return count;
    }
    /**
     * Clear all handlers (useful for testing)
     */
    clear() {
        this.subscribers.clear();
    }
};
exports.EventSubscriberRegistry = EventSubscriberRegistry;
exports.EventSubscriberRegistry = EventSubscriberRegistry = __decorate([
    (0, common_1.Injectable)()
], EventSubscriberRegistry);
//# sourceMappingURL=event-subscriber.registry.js.map