"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageProcessor = void 0;
class MessageProcessor {
    processors = new Map();
    constructor() {
        this.clearProcessors();
    }
    setProcessors(lifecycle, processors) {
        const registrations = processors.map((proc) => ({
            processor: proc,
            priority: 0,
        }));
        registrations.sort((a, b) => b.priority - a.priority);
        this.processors.set(lifecycle, registrations);
    }
    registerProcessor(lifecycle, processor, priority = 0) {
        const registrations = this.processors.get(lifecycle) || [];
        registrations.push({ processor, priority });
        registrations.sort((a, b) => b.priority - a.priority);
        this.processors.set(lifecycle, registrations);
    }
    removeProcessor(lifecycle, processor) {
        const registrations = this.processors.get(lifecycle) || [];
        const filtered = registrations.filter((reg) => reg.processor !== processor);
        this.processors.set(lifecycle, filtered);
    }
    async processMessages(messages, lifecycle) {
        const registrations = this.processors.get(lifecycle) || [];
        if (registrations.length === 0) {
            return messages;
        }
        const modifiedMessages = JSON.parse(JSON.stringify(messages));
        for (const registration of registrations) {
            try {
                await registration.processor(messages, modifiedMessages);
            }
            catch (error) {
                console.error(`Message processor error in ${lifecycle}:`, error);
            }
        }
        return modifiedMessages;
    }
    getProcessorsForLifecycle(lifecycle) {
        const registrations = this.processors.get(lifecycle) || [];
        return registrations.map((reg) => reg.processor);
    }
    clearProcessors(lifecycle) {
        if (lifecycle) {
            this.processors.set(lifecycle, []);
        }
        else {
            this.processors.clear();
            this.processors.set("initial_call", []);
            this.processors.set("pre_call", []);
            this.processors.set("post_call", []);
            this.processors.set("pre_tools", []);
            this.processors.set("post_tools", []);
        }
    }
}
exports.MessageProcessor = MessageProcessor;
//# sourceMappingURL=MessageProcessor.js.map