"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const createScopeMaps_1 = __importDefault(require("../createScopeMaps"));
const traverse_1 = require("@glas/traverse");
const ast_1 = require("../ast");
const pathFunctions_1 = require("../pathFunctions");
function addIonscriptGlobalReferences(root) {
    let scopes = createScopeMaps_1.default(root);
    return traverse_1.traverse(root, {
        enter(node) {
            if (ast_1.Reference.is(node)) {
                return traverse_1.skip;
            }
        },
        leave(node) {
            if (ast_1.Reference.is(node)) {
                let scope = scopes.get(node);
                let declarator = scope[node.name];
                if (declarator == null) {
                    // if we cannot find a declarator then this must be a global reference
                    return pathFunctions_1.getGlobalReference(node);
                }
            }
        }
    });
}
exports.default = addIonscriptGlobalReferences;
