import { checkBannedBuiltinCalls, checkStaticMutation, } from "./staticInitRules.js";
export function validateStaticInit(program, errors) {
    // First sweep: collect static names so the mutation rule has
    // something to match against. Both `static const` and `with handler
    // { static const ... }` shapes count; the `withModifier` wrapper
    // here mirrors how `sectionAssembler` / `initDepGraph` peel it off.
    const staticNames = {};
    for (const node of program.nodes) {
        const inner = node.type === "withModifier" ? node.statement : node;
        if (inner.type !== "assignment")
            continue;
        if (inner.static) {
            staticNames[inner.variableName] = true;
        }
    }
    // Second sweep: validate static initializers + bare statements,
    // and flag obvious mutations against known statics elsewhere at
    // top level.
    for (const node of program.nodes) {
        const inner = node.type === "withModifier" ? node.statement : node;
        // `static const x = ...` — validate the initializer expression.
        if (inner.type === "assignment" && inner.static) {
            const a = inner;
            const label = `Static const \`${a.variableName}\``;
            errors.push(...checkBannedBuiltinCalls(a.value, label));
            continue;
        }
        // `static <bare>` — validate the wrapped statement.
        if (inner.type === "staticStatement") {
            errors.push(...checkBannedBuiltinCalls(inner.statement, "Static bare statement"));
            continue;
        }
        // Anything else at top level — could be a mutation of a known
        // static. checkStaticMutation returns null when it isn't.
        const mutationErr = checkStaticMutation(inner, staticNames);
        if (mutationErr)
            errors.push(mutationErr);
    }
}
