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__ArrayFind = ____lualib.__TS__ArrayFind
local ____exports = {}
local markUsedItemOnThisFrame, v
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local ModCallback = ____isaac_2Dtypescript_2Ddefinitions.ModCallback
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____ModCallbackCustom = require("enums.ModCallbackCustom")
local ModCallbackCustom = ____ModCallbackCustom.ModCallbackCustom
local ____entitiesSpecific = require("functions.entitiesSpecific")
local getPickups = ____entitiesSpecific.getPickups
local ____playerDataStructures = require("functions.playerDataStructures")
local defaultMapGetPlayer = ____playerDataStructures.defaultMapGetPlayer
local mapSetPlayer = ____playerDataStructures.mapSetPlayer
local ____DefaultMap = require("classes.DefaultMap")
local DefaultMap = ____DefaultMap.DefaultMap
local ____CustomCallback = require("classes.private.CustomCallback")
local CustomCallback = ____CustomCallback.CustomCallback
function markUsedItemOnThisFrame(self, player)
    local gameFrameCount = game:GetFrameCount()
    mapSetPlayer(nil, v.room.playersUsedItemOnFrame, player, gameFrameCount)
end
v = {room = {
    playersHoldingItemOnLastFrameMap = __TS__New(DefaultMap, false),
    playersUsedItemOnFrame = __TS__New(DefaultMap, 0)
}}
____exports.PostPurchase = __TS__Class()
local PostPurchase = ____exports.PostPurchase
PostPurchase.name = "PostPurchase"
__TS__ClassExtends(PostPurchase, CustomCallback)
function PostPurchase.prototype.____constructor(self)
    CustomCallback.prototype.____constructor(self)
    self.v = v
    self.shouldFire = function(____, fireArgs, optionalArgs)
        local _player, pickup = table.unpack(fireArgs)
        local callbackPickupVariant, callbackPickupSubType = table.unpack(optionalArgs)
        return (callbackPickupVariant == nil or callbackPickupVariant == pickup.Variant) and (callbackPickupSubType == nil or callbackPickupSubType == pickup.SubType)
    end
    self.postUseItem = function(____, _collectibleType, _rng, player)
        markUsedItemOnThisFrame(nil, player)
        return nil
    end
    self.postUseCard = function(____, _cardType, player)
        markUsedItemOnThisFrame(nil, player)
        return nil
    end
    self.postUsePill = function(____, _pillEffect, player)
        markUsedItemOnThisFrame(nil, player)
        return nil
    end
    self.postPEffectUpdateReordered = function(____, player)
        local isHoldingItem = player:IsHoldingItem()
        local wasHoldingItemOnLastFrame = defaultMapGetPlayer(nil, v.room.playersHoldingItemOnLastFrameMap, player)
        mapSetPlayer(nil, v.room.playersHoldingItemOnLastFrameMap, player, isHoldingItem)
        if not wasHoldingItemOnLastFrame and isHoldingItem and not self:playerUsedItemRecently(player) then
            self:playerPickedUpNewItem(player)
        end
    end
    self.callbacksUsed = {{ModCallback.POST_USE_ITEM, self.postUseItem}, {ModCallback.POST_USE_CARD, self.postUseCard}, {ModCallback.POST_USE_PILL, self.postUsePill}}
    self.customCallbacksUsed = {{ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED, self.postPEffectUpdateReordered}}
end
function PostPurchase.prototype.playerUsedItemRecently(self, player)
    local gameFrameCount = game:GetFrameCount()
    local usedCollectibleOnFrame = defaultMapGetPlayer(nil, v.room.playersUsedItemOnFrame, player)
    return gameFrameCount == usedCollectibleOnFrame or gameFrameCount == usedCollectibleOnFrame + 1
end
function PostPurchase.prototype.playerPickedUpNewItem(self, player)
    local pickups = getPickups(nil)
    local disappearingPickup = __TS__ArrayFind(
        pickups,
        function(____, pickup) return not pickup:Exists() and pickup.Price ~= 0 end
    )
    if disappearingPickup ~= nil then
        self:fire(player, disappearingPickup)
    end
end
return ____exports
