/**
 * Finalize Output
 *
 * NATIVE BRIDGE: Delegates ALL finalization (Phase 8/9/10) to Rust (`native/compiler-native/src/finalize.rs`).
 */
let native;
try {
    try {
        native = require('../../native/compiler-native');
    }
    catch {
        native = require('../../native/compiler-native/index.js');
    }
}
catch (e) {
    // Bridge load handled elsewhere
}
/**
 * Finalize compiler output (Native Bridge)
 *
 * @param ir - Intermediate representation
 * @param compiled - Compiled template
 * @returns Finalized output
 */
export async function finalizeOutput(ir, compiled) {
    if (native && native.finalizeOutputNative) {
        try {
            const result = native.finalizeOutputNative(ir, compiled);
            return {
                html: result.html,
                js: result.js,
                npmImports: result.npmImports,
                styles: result.styles,
                hasErrors: result.hasErrors,
                errors: result.errors,
                bundlePlan: result.bundlePlan
            };
        }
        catch (error) {
            return {
                html: '',
                js: '',
                npmImports: '',
                styles: [],
                hasErrors: true,
                errors: [`Native finalization failed: ${error.message}`]
            };
        }
    }
    throw new Error(`[Zenith Native] Finalize bridge unavailable - cannot finalize ${ir.filePath}`);
}
/**
 * Generate final output with error handling
 */
export async function finalizeOutputOrThrow(ir, compiled) {
    const output = await finalizeOutput(ir, compiled);
    if (output.hasErrors) {
        const errorMessage = output.errors.join('\n\n');
        throw new Error(`Compilation failed:\n\n${errorMessage}`);
    }
    return output;
}
