/**
 * Transform IR to Runtime Code
 *
 * NATIVE BRIDGE: Delegates ALL compilation to Rust (`native/compiler-native/src/codegen.rs`).
 */
let native;
try {
    try {
        native = require('../../native/compiler-native');
    }
    catch {
        native = require('../../native/compiler-native/index.js');
    }
}
catch (e) {
    // Bridge load handled elsewhere
}
/**
 * Analyze ZenIR and emit runtime JavaScript code via Native Bridge
 */
export async function analyzeAndEmit(ir) {
    const scriptContent = ir.script?.raw || '';
    if (!native || !native.generateRuntimeCode) {
        throw new Error(`[Zenith Native] Runtime codegen bridge unavailable`);
    }
    const codegenInput = {
        filePath: ir.filePath,
        scriptContent: scriptContent,
        expressions: ir.template.expressions,
        styles: ir.styles.map(s => ({ raw: s.raw })),
        templateBindings: collectTemplateBindings(ir.template.nodes),
        location: ir.filePath,
        nodes: ir.template.nodes,
        pageBindings: ir.pageBindings || [],
    };
    try {
        return native.generateRuntimeCode(JSON.stringify(codegenInput));
    }
    catch (e) {
        throw new Error(`[Zenith Native] Codegen failed: ${e.message}`);
    }
}
/**
 * Collect template bindings (loop item/index variables) for native codegen
 */
function collectTemplateBindings(nodes) {
    const bindings = [];
    function walk(nodeList) {
        for (const node of nodeList) {
            if (node.type === 'loop-fragment') {
                bindings.push(node.itemVar);
                if (node.indexVar)
                    bindings.push(node.indexVar);
                walk(node.body);
            }
            else if (node.type === 'element') {
                walk(node.children);
            }
            else if (node.type === 'conditional-fragment') {
                walk(node.consequent);
                walk(node.alternate);
            }
            else if (node.type === 'optional-fragment') {
                walk(node.fragment);
            }
        }
    }
    walk(nodes);
    return bindings;
}
