const eventHandlersMap = new Map();
const defaultPriority = 5000;
/**
 * Registers an event handler configuration
 * Stores the configuration in the internal map sorted by priority
 * Lower priority values execute first
 *
 * @param config - The event subscription configuration to register
 * @template T - The type of the event object
 */
export function subscribe(config) {
    const priority = config.priority ?? defaultPriority;
    const eventName = config.eventName;
    if (!eventHandlersMap.has(eventName)) {
        eventHandlersMap.set(eventName, []);
    }
    const handlers = eventHandlersMap.get(eventName);
    let insertIndex = handlers.length;
    for (let i = 0; i < handlers.length; i++) {
        if (priority < handlers[i].priority) {
            insertIndex = i;
            break;
        }
    }
    handlers.splice(insertIndex, 0, {
        eventHandler: config.eventHandler,
        priority,
    });
}
/**
 * Triggers all registered event handlers for a specific event
 * Executes handlers in priority order (lower priority first)
 *
 * @param eventName - The name of the event to trigger
 * @param event - The event object to pass to handlers
 * @template T - The type of the event object
 */
export function onEvent(eventName, event) {
    const handlers = eventHandlersMap.get(eventName);
    if (!handlers) {
        return;
    }
    for (const storedHandler of handlers) {
        storedHandler.eventHandler.func(event);
    }
}
