local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local GameStateFlag = ____isaac_2Dtypescript_2Ddefinitions.GameStateFlag
local GridRoom = ____isaac_2Dtypescript_2Ddefinitions.GridRoom
local LevelCurse = ____isaac_2Dtypescript_2Ddefinitions.LevelCurse
local LevelStage = ____isaac_2Dtypescript_2Ddefinitions.LevelStage
local StageType = ____isaac_2Dtypescript_2Ddefinitions.StageType
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____curses = require("functions.curses")
local hasCurse = ____curses.hasCurse
local ____roomData = require("functions.roomData")
local getRoomGridIndex = ____roomData.getRoomGridIndex
local ____stage = require("functions.stage")
local calculateStageType = ____stage.calculateStageType
local calculateStageTypeRepentance = ____stage.calculateStageTypeRepentance
local onRepentanceStage = ____stage.onRepentanceStage
--- Helper function to get the stage that a trapdoor or heaven door would take the player to, based
-- on the current stage, room, and game state flags.
-- 
-- If you want to account for the player having visited Repentance floors in The Ascent, use the
-- `getNextStageUsingHistory` helper function instead (from the stage history feature). Handling
-- this requires stateful tracking as the player progresses through the run.
function ____exports.getNextStage(self)
    local level = game:GetLevel()
    local backwardsPath = game:GetStateFlag(GameStateFlag.BACKWARDS_PATH)
    local mausoleumHeartKilled = game:GetStateFlag(GameStateFlag.MAUSOLEUM_HEART_KILLED)
    local stage = level:GetStage()
    local repentanceStage = onRepentanceStage(nil)
    local roomGridIndex = getRoomGridIndex(nil)
    if backwardsPath then
        local nextStage = stage - 1
        return nextStage == 0 and LevelStage.HOME or nextStage
    end
    repeat
        local ____switch4 = roomGridIndex
        local ____cond4 = ____switch4 == GridRoom.BLUE_WOMB
        if ____cond4 then
            do
                return LevelStage.BLUE_WOMB
            end
        end
        ____cond4 = ____cond4 or ____switch4 == GridRoom.VOID
        if ____cond4 then
            do
                return LevelStage.VOID
            end
        end
        ____cond4 = ____cond4 or ____switch4 == GridRoom.SECRET_EXIT
        if ____cond4 then
            do
                if repentanceStage then
                    return stage + 1
                end
                if stage == LevelStage.DEPTHS_2 or stage == LevelStage.DEPTHS_1 and hasCurse(nil, LevelCurse.LABYRINTH) then
                    return LevelStage.DEPTHS_2
                end
                return stage
            end
        end
        do
            do
                break
            end
        end
    until true
    if repentanceStage and stage == LevelStage.BASEMENT_2 then
        return LevelStage.CAVES_2
    end
    if repentanceStage and stage == LevelStage.CAVES_2 then
        return LevelStage.DEPTHS_2
    end
    if repentanceStage and stage == LevelStage.DEPTHS_2 then
        if mausoleumHeartKilled then
            return LevelStage.WOMB_1
        end
        return LevelStage.WOMB_2
    end
    if stage == LevelStage.WOMB_2 then
        return LevelStage.SHEOL_CATHEDRAL
    end
    if stage == LevelStage.DARK_ROOM_CHEST then
        return LevelStage.DARK_ROOM_CHEST
    end
    if stage == LevelStage.VOID then
        return LevelStage.VOID
    end
    return stage + 1
end
--- Helper function to get the stage type that a trapdoor or heaven door would take the player to,
-- based on the current stage, room, and game state flags.
-- 
-- If you want to account for previous floors visited on The Ascent, use the
-- `getNextStageTypeUsingHistory` helper function instead (from the stage history feature). Handling
-- this requires stateful tracking as the player progresses through the run.
-- 
-- @param upwards Whether the player should go up to Cathedral in the case of being on Womb 2.
-- Default is false.
function ____exports.getNextStageType(self, upwards)
    if upwards == nil then
        upwards = false
    end
    local backwardsPath = game:GetStateFlag(GameStateFlag.BACKWARDS_PATH)
    local mausoleumHeartKilled = game:GetStateFlag(GameStateFlag.MAUSOLEUM_HEART_KILLED)
    local level = game:GetLevel()
    local stage = level:GetStage()
    local stageType = level:GetStageType()
    local repentanceStage = onRepentanceStage(nil)
    local roomGridIndex = getRoomGridIndex(nil)
    local nextStage = ____exports.getNextStage(nil)
    if backwardsPath then
        return calculateStageType(nil, nextStage)
    end
    if roomGridIndex == GridRoom.SECRET_EXIT then
        return calculateStageTypeRepentance(nil, nextStage)
    end
    if repentanceStage and (stage == LevelStage.BASEMENT_1 or stage == LevelStage.CAVES_1 or stage == LevelStage.DEPTHS_1 or stage == LevelStage.WOMB_1) then
        return calculateStageTypeRepentance(nil, nextStage)
    end
    if repentanceStage and stage == LevelStage.DEPTHS_2 and mausoleumHeartKilled then
        return calculateStageTypeRepentance(nil, nextStage)
    end
    if nextStage == LevelStage.BLUE_WOMB then
        return StageType.ORIGINAL
    end
    if nextStage == LevelStage.SHEOL_CATHEDRAL then
        if upwards then
            return StageType.WRATH_OF_THE_LAMB
        end
        return StageType.ORIGINAL
    end
    if nextStage == LevelStage.DARK_ROOM_CHEST then
        if stageType == StageType.ORIGINAL then
            return StageType.ORIGINAL
        end
        return StageType.WRATH_OF_THE_LAMB
    end
    if nextStage == LevelStage.VOID then
        return StageType.ORIGINAL
    end
    if nextStage == LevelStage.HOME then
        return StageType.ORIGINAL
    end
    return calculateStageType(nil, nextStage)
end
return ____exports
