local ____lualib = require("lualib_bundle")
local Set = ____lualib.Set
local __TS__New = ____lualib.__TS__New
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local __TS__Iterator = ____lualib.__TS__Iterator
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local GridCollisionClass = ____isaac_2Dtypescript_2Ddefinitions.GridCollisionClass
local ModCallback = ____isaac_2Dtypescript_2Ddefinitions.ModCallback
local ____gridEntities = require("functions.gridEntities")
local getCollidingEntitiesWithGridEntity = ____gridEntities.getCollidingEntitiesWithGridEntity
local getGridEntities = ____gridEntities.getGridEntities
local ____DefaultMap = require("classes.DefaultMap")
local DefaultMap = ____DefaultMap.DefaultMap
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
local v = {room = {collidingEntitiesMap = __TS__New(
    DefaultMap,
    function() return __TS__New(Set) end
)}}
____exports.GridEntityCollisionDetection = __TS__Class()
local GridEntityCollisionDetection = ____exports.GridEntityCollisionDetection
GridEntityCollisionDetection.name = "GridEntityCollisionDetection"
__TS__ClassExtends(GridEntityCollisionDetection, Feature)
function GridEntityCollisionDetection.prototype.____constructor(self, postGridEntityCollision, postGridEntityCustomCollision, customGridEntities)
    Feature.prototype.____constructor(self)
    self.v = v
    self.postUpdate = function()
        local gridEntities = getGridEntities(nil)
        local gridEntitiesWithCollision = __TS__ArrayFilter(
            gridEntities,
            function(____, gridEntity) return gridEntity.CollisionClass ~= GridCollisionClass.NONE end
        )
        for ____, gridEntity in ipairs(gridEntitiesWithCollision) do
            local gridEntityPtrHash = GetPtrHash(gridEntity)
            local oldCollidingEntities = v.room.collidingEntitiesMap:getAndSetDefault(gridEntityPtrHash)
            local collidingEntities = getCollidingEntitiesWithGridEntity(nil, gridEntity)
            for ____, entity in ipairs(collidingEntities) do
                local entityPtrHash = GetPtrHash(entity)
                if not oldCollidingEntities:has(entityPtrHash) then
                    oldCollidingEntities:add(entityPtrHash)
                    local gridEntityTypeCustom = self.customGridEntities:getCustomGridEntityType(gridEntity)
                    if gridEntityTypeCustom == nil then
                        self.postGridEntityCollision:fire(gridEntity, entity)
                    else
                        self.postGridEntityCustomCollision:fire(gridEntity, gridEntityTypeCustom, entity)
                    end
                end
            end
            local collidingEntitiesPtrHashes = __TS__ArrayMap(
                collidingEntities,
                function(____, entity) return GetPtrHash(entity) end
            )
            local collidingEntitiesPtrHashSet = __TS__New(Set, collidingEntitiesPtrHashes)
            for ____, oldCollidingEntityPtrHash in __TS__Iterator(oldCollidingEntities) do
                if not collidingEntitiesPtrHashSet:has(oldCollidingEntityPtrHash) then
                    oldCollidingEntities:delete(oldCollidingEntityPtrHash)
                end
            end
        end
    end
    self.callbacksUsed = {{ModCallback.POST_UPDATE, self.postUpdate}}
    self.postGridEntityCollision = postGridEntityCollision
    self.postGridEntityCustomCollision = postGridEntityCustomCollision
    self.customGridEntities = customGridEntities
end
return ____exports
