"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEventManager = void 0;
const logger_1 = require("./logger");
const createEventManager = () => {
    const handlers = {};
    const emit = async (event, file) => {
        const eventHandlers = handlers[event.topic] ?? [];
        const { logger, ...rest } = event;
        logger.debug('[Flow Emit] Event emitted', { handlers: eventHandlers.length, data: rest, file });
        eventHandlers.map((eventHandler) => eventHandler.handler(event));
    };
    const subscribe = (config) => {
        const { event, handlerName, handler, filePath } = config;
        if (!handlers[event]) {
            handlers[event] = [];
        }
        logger_1.globalLogger.debug('[Flow Sub] Subscribing to event', { event, handlerName });
        handlers[event].push({ filePath, handler: handler });
    };
    const unsubscribe = (config) => {
        const { filePath, event } = config;
        handlers[event] = handlers[event]?.filter((handler) => handler.filePath !== filePath);
    };
    return { emit, subscribe, unsubscribe };
};
exports.createEventManager = createEventManager;
