/**
 * Template Engine Registry
 *
 * Manages multiple template engines and provides a unified
 * interface for rendering templates.
 */
import { TypeScriptTemplateEngine } from './typescript-engine.js';
/**
 * Engine not found error
 */
export class EngineNotFoundError extends Error {
    engineType;
    constructor(engineType) {
        super(`Template engine "${engineType}" not found in registry`);
        this.engineType = engineType;
        this.name = 'EngineNotFoundError';
    }
}
/**
 * Template engine registry
 */
export class TemplateEngineRegistry {
    engines = new Map();
    config;
    constructor(config = {}) {
        this.config = config;
        // Register built-in engines
        this.registerDefaultEngines();
        // Register custom engines if provided
        if (config.customEngines) {
            for (const [type, engine] of config.customEngines.entries()) {
                this.register(type, engine);
            }
        }
    }
    /**
     * Register default built-in engines
     */
    registerDefaultEngines() {
        // Register TypeScript engine
        const tsEngine = new TypeScriptTemplateEngine({
            baseDir: this.config.baseDir,
            enableCache: this.config.enableCache
        });
        this.register('typescript', tsEngine);
        // Handlebars and AI engines will be registered in future phases
        // this.register('handlebars', new HandlebarsTemplateEngine(...));
        // this.register('ai', new AITemplateEngine(...));
    }
    /**
     * Register a template engine
     */
    register(type, engine) {
        this.engines.set(type, engine);
    }
    /**
     * Get a template engine by type
     */
    get(type) {
        const engine = this.engines.get(type);
        if (!engine) {
            throw new EngineNotFoundError(type);
        }
        return engine;
    }
    /**
     * Check if an engine type is registered
     */
    has(type) {
        return this.engines.has(type);
    }
    /**
     * Get all registered engine types
     */
    getRegisteredTypes() {
        return Array.from(this.engines.keys());
    }
    /**
     * Render a template using the appropriate engine
     */
    async render(template, context, options) {
        const engine = this.get(template.engine);
        // If basePath is provided and engine is TypeScript, temporarily override baseDir
        if (options?.basePath && 'baseDir' in engine) {
            const originalBaseDir = engine.baseDir;
            engine.baseDir = options.basePath;
            try {
                return await engine.render(template, context);
            }
            finally {
                engine.baseDir = originalBaseDir;
            }
        }
        return await engine.render(template, context);
    }
    /**
     * Render multiple templates in parallel
     */
    async renderMany(templates) {
        const promises = templates.map(({ template, context }) => this.render(template, context));
        return await Promise.all(promises);
    }
    /**
     * Clear all engine caches
     */
    clearCaches() {
        for (const engine of this.engines.values()) {
            if ('clearCache' in engine && typeof engine.clearCache === 'function') {
                engine.clearCache();
            }
        }
    }
    /**
     * Get statistics about registered engines
     */
    getStats() {
        const engineStats = {};
        for (const [type, engine] of this.engines.entries()) {
            if ('getCacheStats' in engine && typeof engine.getCacheStats === 'function') {
                engineStats[type] = engine.getCacheStats();
            }
            else {
                engineStats[type] = { cached: false };
            }
        }
        return {
            registeredEngines: this.getRegisteredTypes(),
            engineStats
        };
    }
}
/**
 * Create a default template engine registry
 */
export function createDefaultRegistry(config) {
    return new TemplateEngineRegistry(config);
}
/**
 * Global singleton registry (lazy-initialized)
 */
let globalRegistry = null;
/**
 * Get the global template engine registry
 */
export function getGlobalRegistry() {
    if (!globalRegistry) {
        globalRegistry = createDefaultRegistry();
    }
    return globalRegistry;
}
/**
 * Set the global template engine registry
 */
export function setGlobalRegistry(registry) {
    globalRegistry = registry;
}
/**
 * Reset the global registry to null
 */
export function resetGlobalRegistry() {
    globalRegistry = null;
}
//# sourceMappingURL=engine-registry.js.map