"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setTracebackFunctionsGlobal = exports.setLogFunctionsGlobal = exports.logNewGlobals = exports.getNewGlobals = exports.getDefaultGlobals = void 0;
const ReadonlySet_1 = require("../types/ReadonlySet");
const debugFunctions_1 = require("./debugFunctions");
const logExports = __importStar(require("./log"));
const log_1 = require("./log");
const logEntitiesExports = __importStar(require("./logEntities"));
const logMiscExports = __importStar(require("./logMisc"));
const set_1 = require("./set");
const sort_1 = require("./sort");
const DEFAULT_GLOBALS = new ReadonlySet_1.ReadonlySet([
    "ActionTriggers",
    "ActiveSlot",
    "BabySubType",
    "BackdropType",
    "BatterySubType",
    "BedSubType",
    "BitSet128",
    "BombSubType",
    "BombVariant",
    "ButtonAction",
    "CacheFlag",
    "Card",
    "Challenge",
    "ChampionColor",
    "ChestSubType",
    "CoinSubType",
    "CollectibleType",
    "Color",
    "CppContainer",
    "DamageFlag",
    "Difficulty",
    "Direction",
    "DoorSlot",
    "DoorState",
    "DoorVariant",
    "EffectVariant",
    "Entity",
    "EntityBomb",
    "EntityCollisionClass",
    "EntityEffect",
    "EntityFamiliar",
    "EntityFlag",
    "EntityGridCollisionClass",
    "EntityKnife",
    "EntityLaser",
    "EntityNPC",
    "EntityPartition",
    "EntityPickup",
    "EntityPlayer",
    "EntityProjectile",
    "EntityPtr",
    "EntityRef",
    "EntityTear",
    "EntityType",
    "FamiliarVariant",
    "Font",
    "Game",
    "GameStateFlag",
    "GetPtrHash",
    "GridCollisionClass",
    "GridEntity",
    "GridEntityDesc",
    "GridEntityDoor",
    "GridEntityPit",
    "GridEntityPoop",
    "GridEntityPressurePlate",
    "GridEntityRock",
    "GridEntitySpikes",
    "GridEntityTNT",
    "GridEntityType",
    "GridRooms",
    "HUD",
    "HeartSubType",
    "Input",
    "InputHook",
    "Isaac",
    "ItemConfig",
    "ItemPool",
    "ItemPoolType",
    "ItemType",
    "KColor",
    "KeySubType",
    "Keyboard",
    "LaserOffset",
    "LaserSubType",
    "Level",
    "LevelCurse",
    "LevelStage",
    "LevelStateFlag",
    "LocustSubtypes",
    "ModCallbacks",
    "Mouse",
    "Music",
    "MusicManager",
    "NpcState",
    "NullItemID",
    "Options",
    "PathFinder",
    "PickupPrice",
    "PickupVariant",
    "PillColor",
    "PillEffect",
    "PlayerForm",
    "PlayerSpriteLayer",
    "PlayerType",
    "PlayerTypes",
    "PoopPickupSubType",
    "PoopSpellType",
    "ProjectileFlags",
    "ProjectileParams",
    "ProjectileVariant",
    "QueueItemData",
    "REPENTANCE",
    "RNG",
    "Random",
    "RandomVector",
    "RegisterMod",
    "RenderMode",
    "Room",
    "RoomConfig",
    "RoomDescriptor",
    "RoomShape",
    "RoomTransitionAnim",
    "RoomType",
    "SFXManager",
    "SackSubType",
    "SeedEffect",
    "Seeds",
    "SkinColor",
    "SortingLayer",
    "SoundEffect",
    "Sprite",
    "StageType",
    "StartDebug",
    "TearFlags",
    "TearParams",
    "TearVariant",
    "TemporaryEffect",
    "TemporaryEffects",
    "TrinketType",
    "UseFlag",
    "Vector",
    "WeaponType",
    "_G",
    "_VERSION",
    "assert",
    "collectgarbage",
    "coroutine",
    "error",
    "getmetatable",
    "include",
    "ipairs",
    "load",
    "math",
    "next",
    "pairs",
    "pcall",
    "print",
    "rawequal",
    "rawget",
    "rawlen",
    "rawset",
    "require",
    "select",
    "setmetatable",
    "string",
    "table",
    "tonumber",
    "tostring",
    "type",
    "utf8",
    "xpcall",
]);
const LUA_DEBUG_ADDED_GLOBALS = new ReadonlySet_1.ReadonlySet([
    "debug",
    "dofile",
    "loadfile",
    "io",
    "os",
    "package",
]);
const RACING_PLUS_SANDBOX_ADDED_GLOBALS = new ReadonlySet_1.ReadonlySet([
    "sandboxTraceback",
    "sandboxGetTraceback",
    "getParentFunctionDescription",
]);
/**
 * Helper function to get a set containing all of the global variable names that are contained
 * within the Isaac environment by default.
 *
 * Returns a slightly different set depending on whether the "--luadebug" flag is enabled.
 */
function getDefaultGlobals() {
    const defaultGlobals = (0, set_1.copySet)(DEFAULT_GLOBALS);
    if ((0, debugFunctions_1.isLuaDebugEnabled)()) {
        (0, set_1.addSetsToSet)(defaultGlobals, LUA_DEBUG_ADDED_GLOBALS);
    }
    if (isRacingPlusSandboxEnabled()) {
        (0, set_1.addSetsToSet)(defaultGlobals, RACING_PLUS_SANDBOX_ADDED_GLOBALS);
    }
    return defaultGlobals;
}
exports.getDefaultGlobals = getDefaultGlobals;
function isRacingPlusSandboxEnabled() {
    return SandboxGetParentFunctionDescription !== undefined;
}
/**
 * Helper function to get an array of any added global variables in the Isaac Lua environment.
 * Returns a sorted array of key/value tuples.
 */
function getNewGlobals() {
    const defaultGlobals = getDefaultGlobals();
    const newGlobals = [];
    for (const [key, value] of pairs(_G)) {
        if (!defaultGlobals.has(key)) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const keyValueTuple = [key, value];
            newGlobals.push(keyValueTuple);
        }
    }
    newGlobals.sort(sort_1.sortTwoDimensionalArray);
    return newGlobals;
}
exports.getNewGlobals = getNewGlobals;
/** Helper function to log any added global variables in the Isaac Lua environment. */
function logNewGlobals() {
    const newGlobals = getNewGlobals();
    (0, log_1.log)("List of added global variables in the Isaac environment:");
    if (newGlobals.length === 0) {
        (0, log_1.log)("- n/a (no extra global variables found)");
    }
    else {
        for (const [i, tuple] of newGlobals.entries()) {
            const [key, value] = tuple;
            // eslint-disable-next-line @typescript-eslint/no-base-to-string
            (0, log_1.log)(`${i + 1}) ${key} - ${value}`);
        }
    }
}
exports.logNewGlobals = logNewGlobals;
/**
 * Converts every `isaacscript-common` function that begins with "log" to a global function.
 *
 * This is useful when printing out variables from the in-game debug console.
 */
function setLogFunctionsGlobal() {
    const globals = _G;
    for (const exports of [logExports, logMiscExports, logEntitiesExports]) {
        // eslint-disable-next-line isaacscript/no-object-any
        for (const [logFuncName, logFunc] of Object.entries(exports)) {
            globals[logFuncName] = logFunc;
        }
    }
}
exports.setLogFunctionsGlobal = setLogFunctionsGlobal;
/**
 * Sets the `traceback` and `getTraceback` functions to be global functions.
 *
 * This is useful when editing Lua files when troubleshooting.
 */
function setTracebackFunctionsGlobal() {
    const globals = _G;
    globals["getTraceback"] = debugFunctions_1.getTraceback;
    globals["traceback"] = debugFunctions_1.traceback;
}
exports.setTracebackFunctionsGlobal = setTracebackFunctionsGlobal;
