local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__DecorateLegacy = ____lualib.__TS__DecorateLegacy
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local CollectibleType = ____isaac_2Dtypescript_2Ddefinitions.CollectibleType
local GridEntityType = ____isaac_2Dtypescript_2Ddefinitions.GridEntityType
local ModCallback = ____isaac_2Dtypescript_2Ddefinitions.ModCallback
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____decorators = require("decorators")
local Exported = ____decorators.Exported
local ____ISCFeature = require("enums.ISCFeature")
local ISCFeature = ____ISCFeature.ISCFeature
local ____ModCallbackCustom = require("enums.ModCallbackCustom")
local ModCallbackCustom = ____ModCallbackCustom.ModCallbackCustom
local ____array = require("functions.array")
local emptyArray = ____array.emptyArray
local ____gridEntities = require("functions.gridEntities")
local getAllGridIndexes = ____gridEntities.getAllGridIndexes
local getGridEntities = ____gridEntities.getGridEntities
local removeGridEntity = ____gridEntities.removeGridEntity
local setGridEntityInvisible = ____gridEntities.setGridEntityInvisible
local spawnGridEntity = ____gridEntities.spawnGridEntity
local ____players = require("functions.players")
local getPlayerFromPtr = ____players.getPlayerFromPtr
local ____roomData = require("functions.roomData")
local getRoomListIndex = ____roomData.getRoomListIndex
local ____DefaultMap = require("classes.DefaultMap")
local DefaultMap = ____DefaultMap.DefaultMap
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
local v = {
    level = {roomListIndexToDecorationGridIndexes = __TS__New(
        DefaultMap,
        function() return {} end
    )},
    room = {manuallyUsingShovel = false}
}
____exports.PreventGridEntityRespawn = __TS__Class()
local PreventGridEntityRespawn = ____exports.PreventGridEntityRespawn
PreventGridEntityRespawn.name = "PreventGridEntityRespawn"
__TS__ClassExtends(PreventGridEntityRespawn, Feature)
function PreventGridEntityRespawn.prototype.____constructor(self, runInNFrames)
    Feature.prototype.____constructor(self)
    self.v = v
    self.preUseItemWeNeedToGoDeeper = function(____, _collectibleType, _rng, player, _useFlags, _activeSlot, _customVarData)
        if v.room.manuallyUsingShovel then
            return nil
        end
        local roomListIndex = getRoomListIndex(nil)
        if not v.level.roomListIndexToDecorationGridIndexes:has(roomListIndex) then
            return
        end
        local decorations = getGridEntities(nil, GridEntityType.DECORATION)
        for ____, decoration in ipairs(decorations) do
            removeGridEntity(nil, decoration, false)
        end
        local entityPtr = EntityPtr(player)
        self.runInNFrames:runNextGameFrame(function()
            local futurePlayer = getPlayerFromPtr(nil, entityPtr)
            if futurePlayer == nil then
                return
            end
            local futureRoomListIndex = getRoomListIndex(nil)
            if futureRoomListIndex ~= roomListIndex then
                return
            end
            v.room.manuallyUsingShovel = true
            futurePlayer:UseActiveItem(CollectibleType.WE_NEED_TO_GO_DEEPER)
            v.room.manuallyUsingShovel = false
            local decorationGridIndexes = v.level.roomListIndexToDecorationGridIndexes:getAndSetDefault(roomListIndex)
            emptyArray(nil, decorationGridIndexes)
            self:preventGridEntityRespawn()
        end)
        return true
    end
    self.postNewRoomReordered = function()
        self:setDecorationsInvisible()
    end
    self.featuresUsed = {ISCFeature.RUN_IN_N_FRAMES}
    self.callbacksUsed = {{ModCallback.PRE_USE_ITEM, self.preUseItemWeNeedToGoDeeper, {CollectibleType.WE_NEED_TO_GO_DEEPER}}}
    self.customCallbacksUsed = {{ModCallbackCustom.POST_NEW_ROOM_REORDERED, self.postNewRoomReordered}}
    self.runInNFrames = runInNFrames
end
function PreventGridEntityRespawn.prototype.setDecorationsInvisible(self)
    local room = game:GetRoom()
    local roomListIndex = getRoomListIndex(nil)
    local decorationGridIndexes = v.level.roomListIndexToDecorationGridIndexes:get(roomListIndex)
    if decorationGridIndexes == nil then
        return
    end
    for ____, gridIndex in ipairs(decorationGridIndexes) do
        local gridEntity = room:GetGridEntity(gridIndex)
        if gridEntity ~= nil then
            local gridEntityType = gridEntity:GetType()
            if gridEntityType == GridEntityType.DECORATION then
                setGridEntityInvisible(nil, gridEntity)
            end
        end
    end
end
function PreventGridEntityRespawn.prototype.preventGridEntityRespawn(self)
    local room = game:GetRoom()
    local roomListIndex = getRoomListIndex(nil)
    local decorationGridIndexes = v.level.roomListIndexToDecorationGridIndexes:getAndSetDefault(roomListIndex)
    for ____, gridIndex in ipairs(getAllGridIndexes(nil)) do
        do
            local existingGridEntity = room:GetGridEntity(gridIndex)
            if existingGridEntity ~= nil then
                goto __continue20
            end
            local decoration = spawnGridEntity(nil, GridEntityType.DECORATION, gridIndex)
            if decoration ~= nil then
                setGridEntityInvisible(nil, decoration)
            end
            decorationGridIndexes[#decorationGridIndexes + 1] = gridIndex
        end
        ::__continue20::
    end
end
__TS__DecorateLegacy({Exported}, PreventGridEntityRespawn.prototype, "preventGridEntityRespawn", true)
return ____exports
