import fs from 'fs-extra';
import Handlebars from 'handlebars';
import { processTemplateVariables } from './serializer.js';
import { resolveAsset } from '../../core/asset-resolver.js';
export class TemplateEngine {
    handlebars;
    constructor() {
        // Create a new Handlebars instance
        this.handlebars = Handlebars.create();
        // Register equality helper for conditional window assignment
        this.handlebars.registerHelper('eq', (a, b) => a === b);
    }
    /**
     * Load template content from file path
     *
     * @param templatePath - Template path (bare name, relative, or absolute)
     */
    async loadTemplate(templatePath) {
        // Use unified asset resolver (works in both local and Docker)
        const resolvedPath = resolveAsset(templatePath, 'template');
        if (!(await fs.pathExists(resolvedPath))) {
            throw new Error(`Template file not found: ${resolvedPath}`);
        }
        return await fs.readFile(resolvedPath, 'utf-8');
    }
    /**
     * Apply template with user code and variable substitution
     */
    applyTemplate(template, userCode, sources, destinations, collector, build) {
        // Process template variables to serialize config objects
        const processedVariables = processTemplateVariables({
            sources,
            destinations,
            collector,
        });
        // Prepare template data for Handlebars
        const templateData = {
            CODE: userCode,
            build: build || {},
            ...processedVariables,
        };
        // Compile and execute the template
        const compiledTemplate = this.handlebars.compile(template);
        return compiledTemplate(templateData);
    }
    /**
     * Process template with user code
     */
    async process(templatePath, userCode, sources, destinations, collector, build) {
        const template = await this.loadTemplate(templatePath);
        return this.applyTemplate(template, userCode, sources, destinations, collector, build);
    }
}
//# sourceMappingURL=template-engine.js.map