local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
local __TS__DecorateLegacy = ____lualib.__TS__DecorateLegacy
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local ____exports = {}
local removeItemsAndTrinketsThatAffectItemPools, restoreItemsAndTrinketsThatAffectItemPools, COLLECTIBLES_THAT_AFFECT_ITEM_POOLS, TRINKETS_THAT_AFFECT_ITEM_POOLS
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local CollectibleType = ____isaac_2Dtypescript_2Ddefinitions.CollectibleType
local ItemConfigTag = ____isaac_2Dtypescript_2Ddefinitions.ItemConfigTag
local PlayerType = ____isaac_2Dtypescript_2Ddefinitions.PlayerType
local TrinketType = ____isaac_2Dtypescript_2Ddefinitions.TrinketType
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 ____collectibleTag = require("functions.collectibleTag")
local collectibleHasTag = ____collectibleTag.collectibleHasTag
local ____playerCollectibles = require("functions.playerCollectibles")
local anyPlayerHasCollectible = ____playerCollectibles.anyPlayerHasCollectible
local ____playerDataStructures = require("functions.playerDataStructures")
local mapGetPlayer = ____playerDataStructures.mapGetPlayer
local mapSetPlayer = ____playerDataStructures.mapSetPlayer
local ____playerIndex = require("functions.playerIndex")
local getAllPlayers = ____playerIndex.getAllPlayers
local ____players = require("functions.players")
local getPlayersOfType = ____players.getPlayersOfType
local ____utils = require("functions.utils")
local ____repeat = ____utils["repeat"]
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
function removeItemsAndTrinketsThatAffectItemPools(self)
    local removedItemsMap = __TS__New(Map)
    local removedTrinketsMap = __TS__New(Map)
    for ____, player in ipairs(getAllPlayers(nil)) do
        local removedItems = {}
        for ____, itemToRemove in ipairs(COLLECTIBLES_THAT_AFFECT_ITEM_POOLS) do
            local numCollectibles = player:GetCollectibleNum(itemToRemove)
            ____repeat(
                nil,
                numCollectibles,
                function()
                    player:RemoveCollectible(itemToRemove)
                    removedItems[#removedItems + 1] = itemToRemove
                end
            )
        end
        mapSetPlayer(nil, removedItemsMap, player, removedItems)
        local removedTrinkets = {}
        for ____, trinketToRemove in ipairs(TRINKETS_THAT_AFFECT_ITEM_POOLS) do
            if player:HasTrinket(trinketToRemove) then
                local numTrinkets = player:GetTrinketMultiplier(trinketToRemove)
                ____repeat(
                    nil,
                    numTrinkets,
                    function()
                        player:TryRemoveTrinket(trinketToRemove)
                        removedTrinkets[#removedTrinkets + 1] = trinketToRemove
                    end
                )
            end
        end
        mapSetPlayer(nil, removedTrinketsMap, player, removedTrinkets)
    end
    return {removedItemsMap = removedItemsMap, removedTrinketsMap = removedTrinketsMap}
end
function restoreItemsAndTrinketsThatAffectItemPools(self, removedItemsMap, removedTrinketsMap)
    for ____, player in ipairs(getAllPlayers(nil)) do
        local removedItems = mapGetPlayer(nil, removedItemsMap, player)
        if removedItems ~= nil then
            for ____, collectibleType in ipairs(removedItems) do
                player:AddCollectible(collectibleType, 0, false)
            end
        end
        local removedTrinkets = mapGetPlayer(nil, removedTrinketsMap, player)
        if removedTrinkets ~= nil then
            for ____, trinketType in ipairs(removedTrinkets) do
                player:AddTrinket(trinketType, false)
            end
        end
    end
end
local COLLECTIBLE_TYPE_THAT_IS_NOT_IN_ANY_POOLS = CollectibleType.KEY_PIECE_1
COLLECTIBLES_THAT_AFFECT_ITEM_POOLS = {CollectibleType.CHAOS, CollectibleType.SACRED_ORB, CollectibleType.TMTRAINER}
TRINKETS_THAT_AFFECT_ITEM_POOLS = {TrinketType.NO}
____exports.ItemPoolDetection = __TS__Class()
local ItemPoolDetection = ____exports.ItemPoolDetection
ItemPoolDetection.name = "ItemPoolDetection"
__TS__ClassExtends(ItemPoolDetection, Feature)
function ItemPoolDetection.prototype.____constructor(self, moddedElementSets)
    Feature.prototype.____constructor(self)
    self.featuresUsed = {ISCFeature.MODDED_ELEMENT_SETS}
    self.moddedElementSets = moddedElementSets
end
function ItemPoolDetection.prototype.getCollectiblesInItemPool(self, itemPoolType)
    local collectibleArray = self.moddedElementSets:getCollectibleTypes()
    return __TS__ArrayFilter(
        collectibleArray,
        function(____, collectibleType) return self:isCollectibleInItemPool(collectibleType, itemPoolType) end
    )
end
__TS__DecorateLegacy({Exported}, ItemPoolDetection.prototype, "getCollectiblesInItemPool", true)
function ItemPoolDetection.prototype.isCollectibleInItemPool(self, collectibleType, itemPoolType)
    if collectibleType == COLLECTIBLE_TYPE_THAT_IS_NOT_IN_ANY_POOLS then
        return false
    end
    local taintedLosts = getPlayersOfType(nil, PlayerType.LOST_B)
    local isOffensive = collectibleHasTag(nil, collectibleType, ItemConfigTag.OFFENSIVE)
    local changedPlayerTypes = false
    if not isOffensive then
        changedPlayerTypes = true
        for ____, player in ipairs(taintedLosts) do
            player:ChangePlayerType(PlayerType.ISAAC)
        end
    end
    local ____removeItemsAndTrinketsThatAffectItemPools_result_0 = removeItemsAndTrinketsThatAffectItemPools(nil)
    local removedItemsMap = ____removeItemsAndTrinketsThatAffectItemPools_result_0.removedItemsMap
    local removedTrinketsMap = ____removeItemsAndTrinketsThatAffectItemPools_result_0.removedTrinketsMap
    local itemPool = game:GetItemPool()
    itemPool:ResetRoomBlacklist()
    for ____, collectibleTypeInSet in ipairs(self.moddedElementSets:getCollectibleTypes()) do
        if collectibleTypeInSet ~= collectibleType then
            itemPool:AddRoomBlacklist(collectibleTypeInSet)
        end
    end
    local seed = 1
    local retrievedCollectibleType = itemPool:GetCollectible(itemPoolType, false, seed, COLLECTIBLE_TYPE_THAT_IS_NOT_IN_ANY_POOLS)
    local collectibleUnlocked = retrievedCollectibleType == collectibleType
    itemPool:ResetRoomBlacklist()
    restoreItemsAndTrinketsThatAffectItemPools(nil, removedItemsMap, removedTrinketsMap)
    if changedPlayerTypes then
        for ____, player in ipairs(taintedLosts) do
            player:ChangePlayerType(PlayerType.LOST_B)
        end
    end
    return collectibleUnlocked
end
__TS__DecorateLegacy({Exported}, ItemPoolDetection.prototype, "isCollectibleInItemPool", true)
function ItemPoolDetection.prototype.isCollectibleUnlocked(self, collectibleType, itemPoolType)
    if anyPlayerHasCollectible(nil, collectibleType) then
        return true
    end
    return self:isCollectibleInItemPool(collectibleType, itemPoolType)
end
__TS__DecorateLegacy({Exported}, ItemPoolDetection.prototype, "isCollectibleUnlocked", true)
return ____exports
