import { AbstractProcessor } from './AbstractProcessor.js';
export class ViewProcessor extends AbstractProcessor {
    process(viewsData) {
        return Object.entries(viewsData).map(([viewName, viewDef]) => {
            const subscriptions = this.parseSubscriptions(viewDef.subscribes_to);
            // Convert uiComponents arrays to objects for schema compliance
            let uiComponents = {};
            if (viewDef.uiComponents) {
                if (Array.isArray(viewDef.uiComponents)) {
                    // Convert array format to object format
                    viewDef.uiComponents.forEach((componentName) => {
                        uiComponents[componentName] = { name: componentName };
                    });
                }
                else if (typeof viewDef.uiComponents === 'object') {
                    // Already an object, pass through
                    uiComponents = viewDef.uiComponents;
                }
            }
            return {
                name: viewName,
                description: viewDef.description,
                type: viewDef.type,
                model: viewDef.model,
                tags: viewDef.tags,
                export: viewDef.export,
                layout: viewDef.layout,
                subscriptions,
                uiComponents,
                properties: viewDef.properties || {}
            };
        });
    }
    parseSubscriptions(subscriptions) {
        // Duplicated logic
        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=ViewProcessor.js.map