"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostPurchase = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../core/cachedClasses");
const ModCallbackCustom_1 = require("../../enums/ModCallbackCustom");
const entitiesSpecific_1 = require("../../functions/entitiesSpecific");
const playerDataStructures_1 = require("../../functions/playerDataStructures");
const DefaultMap_1 = require("../DefaultMap");
const CustomCallback_1 = require("../private/CustomCallback");
const v = {
    room: {
        playersHoldingItemOnLastFrameMap: new DefaultMap_1.DefaultMap(false),
        playersUsedItemOnFrame: new DefaultMap_1.DefaultMap(0),
    },
};
class PostPurchase extends CustomCallback_1.CustomCallback {
    v = v;
    constructor() {
        super();
        this.callbacksUsed = [
            // 3
            [isaac_typescript_definitions_1.ModCallback.POST_USE_ITEM, this.postUseItem],
            // 5
            [isaac_typescript_definitions_1.ModCallback.POST_USE_CARD, this.postUseCard],
            // 10
            [isaac_typescript_definitions_1.ModCallback.POST_USE_PILL, this.postUsePill],
        ];
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
    }
    shouldFire = (fireArgs, optionalArgs) => {
        const [_player, pickup] = fireArgs;
        const [callbackPickupVariant, callbackPickupSubType] = optionalArgs;
        return ((callbackPickupVariant === undefined ||
            callbackPickupVariant === pickup.Variant) &&
            (callbackPickupSubType === undefined ||
                callbackPickupSubType === pickup.SubType));
    };
    // ModCallback.POST_USE_ITEM (3)
    postUseItem = (_collectibleType, _rng, player) => {
        markUsedItemOnThisFrame(player);
        return undefined;
    };
    // ModCallback.POST_USE_CARD (5)
    postUseCard = (_cardType, player) => {
        markUsedItemOnThisFrame(player);
        return undefined;
    };
    // ModCallback.POST_USE_PILL (10)
    postUsePill = (_pillEffect, player) => {
        markUsedItemOnThisFrame(player);
        return undefined;
    };
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        const isHoldingItem = player.IsHoldingItem();
        const wasHoldingItemOnLastFrame = (0, playerDataStructures_1.defaultMapGetPlayer)(v.room.playersHoldingItemOnLastFrameMap, player);
        (0, playerDataStructures_1.mapSetPlayer)(v.room.playersHoldingItemOnLastFrameMap, player, isHoldingItem);
        // Assume that if the player did not use an active item, card, or pill recently, then they
        // purchased an item.
        if (!wasHoldingItemOnLastFrame &&
            isHoldingItem &&
            !this.playerUsedItemRecently(player)) {
            this.playerPickedUpNewItem(player);
        }
    };
    playerUsedItemRecently(player) {
        const gameFrameCount = cachedClasses_1.game.GetFrameCount();
        const usedCollectibleOnFrame = (0, playerDataStructures_1.defaultMapGetPlayer)(v.room.playersUsedItemOnFrame, player);
        return (gameFrameCount === usedCollectibleOnFrame ||
            gameFrameCount === usedCollectibleOnFrame + 1);
    }
    playerPickedUpNewItem(player) {
        const pickups = (0, entitiesSpecific_1.getPickups)();
        const disappearingPickup = pickups.find((pickup) => !pickup.Exists() && pickup.Price !== 0);
        if (disappearingPickup !== undefined) {
            this.fire(player, disappearingPickup);
        }
    }
}
exports.PostPurchase = PostPurchase;
function markUsedItemOnThisFrame(player) {
    const gameFrameCount = cachedClasses_1.game.GetFrameCount();
    (0, playerDataStructures_1.mapSetPlayer)(v.room.playersUsedItemOnFrame, player, gameFrameCount);
}
