local ____lualib = require("lualib_bundle")
local Map = ____lualib.Map
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 ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local ModCallback = ____isaac_2Dtypescript_2Ddefinitions.ModCallback
local PocketItemSlot = ____isaac_2Dtypescript_2Ddefinitions.PocketItemSlot
local ____ModCallbackCustom = require("enums.ModCallbackCustom")
local ModCallbackCustom = ____ModCallbackCustom.ModCallbackCustom
local ____PocketItemType = require("enums.PocketItemType")
local PocketItemType = ____PocketItemType.PocketItemType
local ____pills = require("functions.pills")
local getPillColorFromEffect = ____pills.getPillColorFromEffect
local ____playerDataStructures = require("functions.playerDataStructures")
local mapGetPlayer = ____playerDataStructures.mapGetPlayer
local mapSetPlayer = ____playerDataStructures.mapSetPlayer
local ____pocketItems = require("functions.pocketItems")
local getPocketItems = ____pocketItems.getPocketItems
local pocketItemsEquals = ____pocketItems.pocketItemsEquals
local ____CustomCallback = require("classes.private.CustomCallback")
local CustomCallback = ____CustomCallback.CustomCallback
local v = {run = {
    pillColorToPillEffect = __TS__New(Map),
    playerPocketItems = __TS__New(Map)
}}
--- The vanilla `POST_USE_PILL` callback does not pass the `PillColor` of the used pill. We can
-- resolve pill effect to pill color by using the `ItemPool.GetPillEffect` method. However, this
-- does not tell us whether the pill used was a horse pill. Thus, we must keep track of the pills
-- that the player is holding on every frame to account for this.
-- 
-- In some cases, pills can be used without a corresponding pocket item slot, like in the case of
-- the reverse Temperance card. In this case, we fall back to looking up the color using the
-- `ItemPool.GetPillEffect` method.
____exports.PostUsePillFilter = __TS__Class()
local PostUsePillFilter = ____exports.PostUsePillFilter
PostUsePillFilter.name = "PostUsePillFilter"
__TS__ClassExtends(PostUsePillFilter, CustomCallback)
function PostUsePillFilter.prototype.____constructor(self)
    CustomCallback.prototype.____constructor(self)
    self.v = v
    self.postUsePill = function(____, pillEffect, player, useFlags)
        local pillColor = self:getPillColorOfCurrentlyUsedPill(player, pillEffect)
        self:fire(pillEffect, pillColor, player, useFlags)
    end
    self.postPEffectUpdateReordered = function(____, player)
        self:updateCurrentPocketItems(player)
    end
    self.callbacksUsed = {{ModCallback.POST_USE_PILL, self.postUsePill}}
    self.customCallbacksUsed = {{ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED, self.postPEffectUpdateReordered}}
end
function PostUsePillFilter.prototype.getPillColorOfCurrentlyUsedPill(self, player, pillEffect)
    local oldPocketItems = mapGetPlayer(nil, v.run.playerPocketItems, player)
    if oldPocketItems ~= nil then
        local pocketItems = getPocketItems(nil, player)
        if not pocketItemsEquals(nil, oldPocketItems, pocketItems) then
            local oldPocketItemSlot1 = __TS__ArrayFind(
                oldPocketItems,
                function(____, pocketItem) return pocketItem.slot == PocketItemSlot.SLOT_1 end
            )
            if oldPocketItemSlot1 ~= nil and oldPocketItemSlot1.type == PocketItemType.PILL then
                return oldPocketItemSlot1.subType
            end
        end
    end
    return getPillColorFromEffect(nil, pillEffect)
end
function PostUsePillFilter.prototype.updateCurrentPocketItems(self, player)
    local pocketItems = getPocketItems(nil, player)
    mapSetPlayer(nil, v.run.playerPocketItems, player, pocketItems)
end
return ____exports
