"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostUsePillFilter = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const ModCallbackCustom_1 = require("../../enums/ModCallbackCustom");
const PocketItemType_1 = require("../../enums/PocketItemType");
const pills_1 = require("../../functions/pills");
const playerDataStructures_1 = require("../../functions/playerDataStructures");
const pocketItems_1 = require("../../functions/pocketItems");
const CustomCallback_1 = require("../private/CustomCallback");
const v = {
    run: {
        pillColorToPillEffect: new Map(),
        playerPocketItems: 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.
 */
class PostUsePillFilter extends CustomCallback_1.CustomCallback {
    v = v;
    constructor() {
        super();
        this.callbacksUsed = [
            // 10
            [isaac_typescript_definitions_1.ModCallback.POST_USE_PILL, this.postUsePill],
        ];
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
    }
    // ModCallback.POST_USE_PILL (10)
    postUsePill = (pillEffect, player, useFlags) => {
        const pillColor = this.getPillColorOfCurrentlyUsedPill(player, pillEffect);
        this.fire(pillEffect, pillColor, player, useFlags);
    };
    getPillColorOfCurrentlyUsedPill(player, pillEffect) {
        // First, check to see if the pocket items have changed in some way, which indicates that a real
        // pill was used.
        const oldPocketItems = (0, playerDataStructures_1.mapGetPlayer)(v.run.playerPocketItems, player);
        if (oldPocketItems !== undefined) {
            const pocketItems = (0, pocketItems_1.getPocketItems)(player);
            if (!(0, pocketItems_1.pocketItemsEquals)(oldPocketItems, pocketItems)) {
                const oldPocketItemSlot1 = oldPocketItems.find((pocketItem) => pocketItem.slot === isaac_typescript_definitions_1.PocketItemSlot.SLOT_1);
                if (oldPocketItemSlot1 !== undefined &&
                    oldPocketItemSlot1.type === PocketItemType_1.PocketItemType.PILL) {
                    return oldPocketItemSlot1.subType;
                }
            }
        }
        // At this point, either the pocket items have not changed, or we were not able to find a pill
        // in the old pocket items. The player might be using a reverse Temperance card, so we revert to
        // assuming that a non-horse pill was used and look up the color using the
        // `ItemPool.GetPillEffect` method.
        return (0, pills_1.getPillColorFromEffect)(pillEffect);
    }
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        this.updateCurrentPocketItems(player);
    };
    updateCurrentPocketItems(player) {
        const pocketItems = (0, pocketItems_1.getPocketItems)(player);
        (0, playerDataStructures_1.mapSetPlayer)(v.run.playerPocketItems, player, pocketItems);
    }
}
exports.PostUsePillFilter = PostUsePillFilter;
