local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__DecorateLegacy = ____lualib.__TS__DecorateLegacy
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local EffectVariant = ____isaac_2Dtypescript_2Ddefinitions.EffectVariant
local EntityType = ____isaac_2Dtypescript_2Ddefinitions.EntityType
local ModCallback = ____isaac_2Dtypescript_2Ddefinitions.ModCallback
local ____decorators = require("decorators")
local Exported = ____decorators.Exported
local ____LadderSubTypeCustom = require("enums.LadderSubTypeCustom")
local LadderSubTypeCustom = ____LadderSubTypeCustom.LadderSubTypeCustom
local ____entities = require("functions.entities")
local getEntityID = ____entities.getEntityID
local getEntityIDFromConstituents = ____entities.getEntityIDFromConstituents
local ____entitiesSpecific = require("functions.entitiesSpecific")
local spawnEffect = ____entitiesSpecific.spawnEffect
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
--- Normally, we would make a custom entity to represent a fading-away pickup, but we don't want to
-- interfere with the "entities2.xml" file in end-user mods. Thus, we must select a vanilla effect
-- to masquerade as a backdrop effect.
-- 
-- We arbitrarily choose a ladder for this purpose because it will not automatically despawn after
-- time passes, like most other effects.
local PICKUP_EFFECT_VARIANT = EffectVariant.LADDER
local PICKUP_EFFECT_SUB_TYPE = LadderSubTypeCustom.CUSTOM_PICKUP
____exports.CustomPickups = __TS__Class()
local CustomPickups = ____exports.CustomPickups
CustomPickups.name = "CustomPickups"
__TS__ClassExtends(CustomPickups, Feature)
function CustomPickups.prototype.____constructor(self)
    Feature.prototype.____constructor(self)
    self.customPickupFunctionsMap = __TS__New(Map)
    self.prePickupCollision = function(____, pickup, collider)
        local entityID = getEntityID(nil, pickup)
        local customPickupFunctions = self.customPickupFunctionsMap:get(entityID)
        if customPickupFunctions == nil then
            return nil
        end
        local player = collider:ToPlayer()
        if player == nil then
            return nil
        end
        local shouldPickup = customPickupFunctions.collisionFunc(player)
        if not shouldPickup then
            return nil
        end
        pickup:Remove()
        local pickupSprite = pickup:GetSprite()
        local fileName = pickupSprite:GetFilename()
        local effect = spawnEffect(nil, PICKUP_EFFECT_VARIANT, PICKUP_EFFECT_SUB_TYPE, pickup.Position)
        local effectSprite = effect:GetSprite()
        effectSprite:Load(fileName, true)
        effectSprite:Play("Collect", true)
        customPickupFunctions.collectFunc(player)
        return nil
    end
    self.postEffectRenderPickupEffect = function(____, effect)
        if effect.SubType ~= PICKUP_EFFECT_SUB_TYPE then
            return
        end
        local sprite = effect:GetSprite()
        if sprite:IsFinished("Collect") then
            effect:Remove()
        end
    end
    self.callbacksUsed = {{ModCallback.PRE_PICKUP_COLLISION, self.prePickupCollision}, {ModCallback.POST_EFFECT_RENDER, self.postEffectRenderPickupEffect, {PICKUP_EFFECT_VARIANT}}}
end
function CustomPickups.prototype.registerCustomPickup(self, pickupVariantCustom, subType, collectFunc, collisionFunc)
    if collisionFunc == nil then
        collisionFunc = function() return true end
    end
    local entityID = getEntityIDFromConstituents(nil, EntityType.PICKUP, pickupVariantCustom, subType)
    local customPickupFunctions = {collectFunc = collectFunc, collisionFunc = collisionFunc}
    self.customPickupFunctionsMap:set(entityID, customPickupFunctions)
end
__TS__DecorateLegacy({Exported}, CustomPickups.prototype, "registerCustomPickup", true)
return ____exports
