local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__DecorateLegacy = ____lualib.__TS__DecorateLegacy
local __TS__ArraySome = ____lualib.__TS__ArraySome
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local GameStateFlag = ____isaac_2Dtypescript_2Ddefinitions.GameStateFlag
local LevelStage = ____isaac_2Dtypescript_2Ddefinitions.LevelStage
local StageType = ____isaac_2Dtypescript_2Ddefinitions.StageType
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____decorators = require("decorators")
local Exported = ____decorators.Exported
local ____ModCallbackCustom = require("enums.ModCallbackCustom")
local ModCallbackCustom = ____ModCallbackCustom.ModCallbackCustom
local ____nextStage = require("functions.nextStage")
local getNextStage = ____nextStage.getNextStage
local getNextStageType = ____nextStage.getNextStageType
local ____stage = require("functions.stage")
local calculateStageType = ____stage.calculateStageType
local onRepentanceStage = ____stage.onRepentanceStage
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
local v = {run = {stageHistory = {}}}
____exports.StageHistory = __TS__Class()
local StageHistory = ____exports.StageHistory
StageHistory.name = "StageHistory"
__TS__ClassExtends(StageHistory, Feature)
function StageHistory.prototype.____constructor(self)
    Feature.prototype.____constructor(self)
    self.v = v
    self.postNewLevelReordered = function()
        local level = game:GetLevel()
        local stage = level:GetStage()
        local stageType = level:GetStageType()
        local ____v_run_stageHistory_0 = v.run.stageHistory
        ____v_run_stageHistory_0[#____v_run_stageHistory_0 + 1] = {stage = stage, stageType = stageType}
    end
    self.customCallbacksUsed = {{ModCallbackCustom.POST_NEW_LEVEL_REORDERED, self.postNewLevelReordered}}
end
function StageHistory.prototype.getNextStageTypeWithHistory(self, upwards)
    if upwards == nil then
        upwards = false
    end
    local backwardsPath = game:GetStateFlag(GameStateFlag.BACKWARDS_PATH)
    if not backwardsPath then
        return getNextStageType(nil, upwards)
    end
    local level = game:GetLevel()
    local stage = level:GetStage()
    local repentanceStage = onRepentanceStage(nil)
    local visitedDownpour1 = self:hasVisitedStage(LevelStage.BASEMENT_1, StageType.REPENTANCE)
    local visitedDross1 = self:hasVisitedStage(LevelStage.BASEMENT_1, StageType.REPENTANCE_B)
    local visitedDownpour2 = self:hasVisitedStage(LevelStage.BASEMENT_2, StageType.REPENTANCE)
    local visitedDross2 = self:hasVisitedStage(LevelStage.BASEMENT_2, StageType.REPENTANCE_B)
    local visitedMines1 = self:hasVisitedStage(LevelStage.CAVES_1, StageType.REPENTANCE)
    local visitedAshpit1 = self:hasVisitedStage(LevelStage.CAVES_1, StageType.REPENTANCE_B)
    local visitedMines2 = self:hasVisitedStage(LevelStage.DEPTHS_2, StageType.REPENTANCE)
    local visitedAshpit2 = self:hasVisitedStage(LevelStage.DEPTHS_2, StageType.REPENTANCE_B)
    if stage == LevelStage.BASEMENT_2 and repentanceStage then
        if visitedDownpour1 then
            return StageType.REPENTANCE
        end
        if visitedDross1 then
            return StageType.REPENTANCE_B
        end
    end
    if stage == LevelStage.CAVES_1 and repentanceStage then
        if visitedDownpour2 then
            return StageType.REPENTANCE
        end
        if visitedDross2 then
            return StageType.REPENTANCE_B
        end
    end
    if stage == LevelStage.CAVES_2 and not repentanceStage then
        if visitedDownpour2 then
            return StageType.REPENTANCE
        end
        if visitedDross2 then
            return StageType.REPENTANCE_B
        end
    end
    if stage == LevelStage.CAVES_2 and repentanceStage then
        if visitedMines1 then
            return StageType.REPENTANCE
        end
        if visitedAshpit1 then
            return StageType.REPENTANCE_B
        end
    end
    if stage == LevelStage.DEPTHS_2 and not repentanceStage then
        if visitedAshpit2 then
            return StageType.REPENTANCE_B
        end
        if visitedMines2 then
            return StageType.REPENTANCE
        end
    end
    local nextStage = self:getNextStageWithHistory()
    return calculateStageType(nil, nextStage)
end
__TS__DecorateLegacy({Exported}, StageHistory.prototype, "getNextStageTypeWithHistory", true)
function StageHistory.prototype.getNextStageWithHistory(self)
    local backwardsPath = game:GetStateFlag(GameStateFlag.BACKWARDS_PATH)
    if not backwardsPath then
        return getNextStage(nil)
    end
    local level = game:GetLevel()
    local stage = level:GetStage()
    local repentanceStage = onRepentanceStage(nil)
    local visitedDownpour1 = self:hasVisitedStage(LevelStage.BASEMENT_1, StageType.REPENTANCE)
    local visitedDross1 = self:hasVisitedStage(LevelStage.BASEMENT_1, StageType.REPENTANCE_B)
    local visitedDownpour2 = self:hasVisitedStage(LevelStage.BASEMENT_2, StageType.REPENTANCE)
    local visitedDross2 = self:hasVisitedStage(LevelStage.BASEMENT_2, StageType.REPENTANCE_B)
    local visitedMines1 = self:hasVisitedStage(LevelStage.CAVES_1, StageType.REPENTANCE)
    local visitedAshpit1 = self:hasVisitedStage(LevelStage.CAVES_1, StageType.REPENTANCE_B)
    local visitedMines2 = self:hasVisitedStage(LevelStage.DEPTHS_2, StageType.REPENTANCE)
    local visitedAshpit2 = self:hasVisitedStage(LevelStage.DEPTHS_2, StageType.REPENTANCE_B)
    if stage == LevelStage.BASEMENT_1 then
        if repentanceStage then
            return LevelStage.BASEMENT_1
        end
        return LevelStage.HOME
    end
    if stage == LevelStage.BASEMENT_2 then
        if repentanceStage then
            if visitedDownpour1 or visitedDross1 then
                return LevelStage.BASEMENT_1
            end
            return LevelStage.BASEMENT_2
        end
        return LevelStage.BASEMENT_1
    end
    if stage == LevelStage.CAVES_1 then
        if repentanceStage then
            if visitedDownpour2 or visitedDross2 then
                return LevelStage.BASEMENT_2
            end
            return LevelStage.CAVES_1
        end
        return LevelStage.BASEMENT_2
    end
    if stage == LevelStage.CAVES_2 then
        if repentanceStage then
            if visitedMines1 or visitedAshpit1 then
                return LevelStage.CAVES_1
            end
            return LevelStage.CAVES_2
        end
        return LevelStage.CAVES_1
    end
    if stage == LevelStage.DEPTHS_1 then
        if repentanceStage then
            if visitedMines2 or visitedAshpit2 then
                return LevelStage.CAVES_2
            end
            return LevelStage.DEPTHS_1
        end
        return LevelStage.CAVES_2
    end
    if stage == LevelStage.DEPTHS_2 then
        if repentanceStage then
            return LevelStage.DEPTHS_2
        end
        return LevelStage.DEPTHS_1
    end
    return stage - 1
end
__TS__DecorateLegacy({Exported}, StageHistory.prototype, "getNextStageWithHistory", true)
function StageHistory.prototype.getStageHistory(self)
    return v.run.stageHistory
end
__TS__DecorateLegacy({Exported}, StageHistory.prototype, "getStageHistory", true)
function StageHistory.prototype.hasVisitedStage(self, stage, stageType)
    if stageType == nil then
        return __TS__ArraySome(
            v.run.stageHistory,
            function(____, stageHistoryEntry) return stageHistoryEntry.stage == stage end
        )
    end
    return __TS__ArraySome(
        v.run.stageHistory,
        function(____, stageHistoryEntry) return stageHistoryEntry.stage == stage and stageHistoryEntry.stageType == stageType end
    )
end
__TS__DecorateLegacy({Exported}, StageHistory.prototype, "hasVisitedStage", true)
return ____exports
