import { AbstractProcessor } from './AbstractProcessor.js';
import { ExecutableProcessor } from './ExecutableProcessor.js';
export class ControllerProcessor extends AbstractProcessor {
    executableProcessor;
    constructor(context) {
        super(context);
        this.executableProcessor = new ExecutableProcessor(context);
    }
    process(controllersData) {
        return Object.entries(controllersData).map(([controllerName, controllerDef]) => {
            const subscriptions = this.parseSubscriptions(controllerDef.subscribes_to);
            // Process CURED operations
            const cured = {};
            if (controllerDef.cured) {
                for (const [operation, operationDef] of Object.entries(controllerDef.cured)) {
                    // Special handling for 'validate' - it's not an ExecutableProperties (v3.3+)
                    if (operation === 'validate') {
                        cured[operation] = operationDef;
                    }
                    else {
                        cured[operation] = this.executableProcessor.process(operationDef, `Controller ${controllerName}, CURED ${operation}`);
                    }
                }
            }
            // Process custom actions
            const actions = {};
            if (controllerDef.actions) {
                for (const [actionName, actionDef] of Object.entries(controllerDef.actions)) {
                    actions[actionName] = this.executableProcessor.process(actionDef, `Controller ${controllerName}, Action ${actionName}`);
                }
            }
            return {
                name: controllerName,
                model: controllerDef.model,
                path: controllerDef.path,
                description: controllerDef.description,
                subscriptions,
                cured,
                actions
            };
        });
    }
    parseSubscriptions(subscriptions) {
        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=ControllerProcessor.js.map