import { BUILTIN_FUNCTION_TYPES } from "./builtins.js";
import { JS_GLOBALS } from "./resolveCall.js";
const has = (obj, name) => Object.prototype.hasOwnProperty.call(obj, name);
/**
 * Resolution order for variable references — mirrors `resolveCall` but
 * starts from local scope, since variable references are usually local.
 *
 *   1. Local scope binding              — let/const, params, for-vars, …
 *   2. Local `def`/`node`               — function reference.
 *   3. Imported function/node           — cross-file function reference.
 *   4. `BUILTIN_FUNCTION_TYPES`         — Agency primitive as function ref.
 *   5. JS global (callable or namespace) — covers both bare `parseInt` and
 *                                          namespace bases like `JSON`.
 *   6. Otherwise: unresolved.
 */
export function resolveVariable(name, input) {
    if (input.scopeHas(name))
        return { kind: "scopeBinding" };
    if (has(input.functionDefs, name) || has(input.nodeDefs, name)) {
        return { kind: "def" };
    }
    if (has(input.importedFunctions, name))
        return { kind: "imported" };
    if (input.importedNodeNames.includes(name))
        return { kind: "imported" };
    if (input.jsImportedNames && has(input.jsImportedNames, name))
        return { kind: "jsImported" };
    if (has(BUILTIN_FUNCTION_TYPES, name))
        return { kind: "builtin" };
    if (has(JS_GLOBALS, name))
        return { kind: "jsGlobal" };
    return { kind: "unresolved" };
}
