/**
 * TransformStage - BashTransformPipeline plugin execution.
 *
 * Applies registered transform plugins to the AST, collecting
 * metadata into a null-prototype object to prevent pollution.
 */
import { mergeToNullPrototype } from "../../helpers/env.js";
export class TransformStage {
    name = "transform";
    // biome-ignore lint/suspicious/noExplicitAny: type-erased plugin storage for untyped API
    plugins;
    // biome-ignore lint/suspicious/noExplicitAny: type-erased plugin storage for untyped API
    constructor(plugins) {
        this.plugins = plugins;
    }
    async execute(context) {
        if (this.plugins.length === 0 || context.ast === undefined) {
            return { continue: true, context };
        }
        let ast = context.ast;
        let meta = Object.create(null);
        for (const plugin of this.plugins) {
            const pluginResult = plugin.transform({ ast, metadata: meta });
            ast = pluginResult.ast;
            if (pluginResult.metadata) {
                meta = mergeToNullPrototype(meta, pluginResult.metadata);
            }
        }
        context.ast = ast;
        context.metadata = meta;
        return { continue: true, context };
    }
}
