import { AbstractProcessor } from './AbstractProcessor.js';
import { ExecutableProcessor } from './ExecutableProcessor.js';
export class ServiceProcessor extends AbstractProcessor {
    executableProcessor;
    constructor(context) {
        super(context);
        this.executableProcessor = new ExecutableProcessor(context);
    }
    process(servicesData) {
        return Object.entries(servicesData).map(([serviceName, serviceDef]) => {
            const subscriptions = this.parseSubscriptions(serviceDef.subscribes_to);
            const operations = {};
            if (serviceDef.operations) {
                for (const [opName, opDef] of Object.entries(serviceDef.operations)) {
                    operations[opName] = this.executableProcessor.process(opDef, `Service ${serviceName}, Operation ${opName}`);
                }
            }
            return {
                name: serviceName,
                description: serviceDef.description,
                subscriptions,
                operations
            };
        });
    }
    parseSubscriptions(subscriptions) {
        // Duplicated logic - ideally this moves to a shared helper or base class
        // For now, keeping it here to isolate the processor
        if (!subscriptions) {
            return { events: [], handlers: {} };
        }
        if (Array.isArray(subscriptions)) {
            const events = [];
            const handlers = {};
            for (const item of subscriptions) {
                if (typeof item === 'string') {
                    events.push(item);
                }
                else if (typeof item === 'object') {
                    for (const [event, handler] of Object.entries(item)) {
                        events.push(event);
                        handlers[event] = handler;
                    }
                }
            }
            return { events, handlers };
        }
        else if (typeof subscriptions === 'object') {
            const events = Object.keys(subscriptions);
            const handlers = {};
            for (const [event, handler] of Object.entries(subscriptions)) {
                handlers[event] = handler;
            }
            return { events, handlers };
        }
        return { events: [], handlers: {} };
    }
}
//# sourceMappingURL=ServiceProcessor.js.map