local ____lualib = require("lualib_bundle")
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local CollectibleType = ____isaac_2Dtypescript_2Ddefinitions.CollectibleType
local DamageFlag = ____isaac_2Dtypescript_2Ddefinitions.DamageFlag
local PlayerVariant = ____isaac_2Dtypescript_2Ddefinitions.PlayerVariant
local RoomType = ____isaac_2Dtypescript_2Ddefinitions.RoomType
local TrinketType = ____isaac_2Dtypescript_2Ddefinitions.TrinketType
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____ModCallbackCustom = require("enums.ModCallbackCustom")
local ModCallbackCustom = ____ModCallbackCustom.ModCallbackCustom
local ____flag = require("functions.flag")
local hasFlag = ____flag.hasFlag
local ____frames = require("functions.frames")
local onGameFrame = ____frames.onGameFrame
local ____playerDataStructures = require("functions.playerDataStructures")
local mapGetPlayer = ____playerDataStructures.mapGetPlayer
local mapSetPlayer = ____playerDataStructures.mapSetPlayer
local ____players = require("functions.players")
local getPlayerNumHitsRemaining = ____players.getPlayerNumHitsRemaining
local ____shouldFire = require("shouldFire")
local shouldFirePlayer = ____shouldFire.shouldFirePlayer
local ____CustomCallback = require("classes.private.CustomCallback")
local CustomCallback = ____CustomCallback.CustomCallback
local v = {
    run = {playersDamageFrameMap = __TS__New(Map)},
    level = {numSacrifices = 0}
}
____exports.PostCursedTeleport = __TS__Class()
local PostCursedTeleport = ____exports.PostCursedTeleport
PostCursedTeleport.name = "PostCursedTeleport"
__TS__ClassExtends(PostCursedTeleport, CustomCallback)
function PostCursedTeleport.prototype.____constructor(self)
    CustomCallback.prototype.____constructor(self)
    self.v = v
    self.shouldFire = shouldFirePlayer
    self.entityTakeDmgPlayer = function(____, player, _amount, damageFlags, _source, _countdownFrames)
        self:incrementNumSacrifices(damageFlags)
        self:setDamageFrame(player, damageFlags)
        return nil
    end
    self.postPlayerRenderReorderedPlayer = function(____, player, _renderOffset)
        local trackingArray = mapGetPlayer(nil, v.run.playersDamageFrameMap, player)
        if trackingArray == nil then
            return
        end
        local lastDamageFrame, callbackActivatedOnThisFrame = table.unpack(trackingArray)
        if not self:playerIsTeleportingFromCursedTeleport(player, lastDamageFrame) then
            return
        end
        if callbackActivatedOnThisFrame then
            return
        end
        local gameFrameCount = game:GetFrameCount()
        local newTrackingArray = {gameFrameCount, true}
        mapSetPlayer(nil, v.run.playersDamageFrameMap, player, newTrackingArray)
        self:fire(player)
    end
    self.customCallbacksUsed = {{ModCallbackCustom.ENTITY_TAKE_DMG_PLAYER, self.entityTakeDmgPlayer}, {ModCallbackCustom.POST_PLAYER_RENDER_REORDERED, self.postPlayerRenderReorderedPlayer, {PlayerVariant.PLAYER}}}
end
function PostCursedTeleport.prototype.incrementNumSacrifices(self, damageFlags)
    local room = game:GetRoom()
    local roomType = room:GetType()
    local isSpikeDamage = hasFlag(nil, damageFlags, DamageFlag.SPIKES)
    if roomType == RoomType.SACRIFICE and isSpikeDamage then
        local ____v_level_0, ____numSacrifices_1 = v.level, "numSacrifices"
        ____v_level_0[____numSacrifices_1] = ____v_level_0[____numSacrifices_1] + 1
    end
end
function PostCursedTeleport.prototype.setDamageFrame(self, player, damageFlags)
    local gameFrameCount = game:GetFrameCount()
    local trackingArray = mapGetPlayer(nil, v.run.playersDamageFrameMap, player)
    if trackingArray ~= nil then
        local lastDamageFrame, callbackFiredOnThisFrame = table.unpack(trackingArray)
        if lastDamageFrame == gameFrameCount and callbackFiredOnThisFrame then
            return
        end
    end
    if self:isPotentialNaturalTeleportFromSacrificeRoom(damageFlags) then
        return
    end
    local newTrackingArray = {gameFrameCount, false}
    mapSetPlayer(nil, v.run.playersDamageFrameMap, player, newTrackingArray)
end
function PostCursedTeleport.prototype.isPotentialNaturalTeleportFromSacrificeRoom(self, damageFlags)
    local room = game:GetRoom()
    local roomType = room:GetType()
    local isSpikeDamage = hasFlag(nil, damageFlags, DamageFlag.SPIKES)
    return roomType == RoomType.SACRIFICE and isSpikeDamage and (v.level.numSacrifices == 6 or v.level.numSacrifices >= 12)
end
function PostCursedTeleport.prototype.playerIsTeleportingFromCursedTeleport(self, player, lastDamageFrame)
    if not onGameFrame(nil, lastDamageFrame) then
        return false
    end
    local sprite = player:GetSprite()
    if not sprite:IsPlaying("TeleportUp") or sprite:GetFrame() ~= 1 then
        return false
    end
    if player:HasCollectible(CollectibleType.CURSED_EYE) then
        return true
    end
    local numHitsRemaining = getPlayerNumHitsRemaining(nil, player)
    if player:HasTrinket(TrinketType.CURSED_SKULL) and numHitsRemaining == 1 then
        return true
    end
    return false
end
return ____exports
