"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemPickupDetection = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const playerDataStructures_1 = require("../../../functions/playerDataStructures");
const types_1 = require("../../../functions/types");
const PickingUpItem_1 = require("../../../types/PickingUpItem");
const DefaultMap_1 = require("../../DefaultMap");
const Feature_1 = require("../../private/Feature");
const v = {
    run: {
        playersPickingUpItemMap: new DefaultMap_1.DefaultMap(() => (0, PickingUpItem_1.newPickingUpItem)()),
    },
};
class ItemPickupDetection extends Feature_1.Feature {
    v = v;
    postItemPickup;
    preItemPickup;
    constructor(postItemPickup, preItemPickup) {
        super();
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
        this.postItemPickup = postItemPickup;
        this.preItemPickup = preItemPickup;
    }
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        const pickingUpItem = (0, playerDataStructures_1.defaultMapGetPlayer)(v.run.playersPickingUpItemMap, player);
        if (player.IsItemQueueEmpty()) {
            this.queueEmpty(player, pickingUpItem);
            // If a player enters a room with a trinket next to the entrance, the player will pick up the
            // trinket, but it will not become queued (it will be deposited into their inventory
            // immediately). Since we don't know what type of item the player is holding, don't account
            // for this bug.
        }
        else {
            this.queueNotEmpty(player, pickingUpItem);
        }
    };
    queueEmpty(player, pickingUpItem) {
        if (pickingUpItem.itemType === isaac_typescript_definitions_1.ItemType.NULL ||
            (0, types_1.asNumber)(pickingUpItem.subType) === 0) {
            return;
        }
        this.postItemPickup.fire(player, pickingUpItem);
        (0, PickingUpItem_1.resetPickingUpItem)(pickingUpItem);
    }
    queueNotEmpty(player, pickingUpItem) {
        const queuedItem = player.QueuedItem.Item;
        if (queuedItem === undefined || queuedItem.Type === isaac_typescript_definitions_1.ItemType.NULL) {
            // This should never happen, since the `EntityPlayer.IsItemQueueEmpty` method returned true.
            return;
        }
        if (queuedItem.Type !== pickingUpItem.itemType ||
            queuedItem.ID !== pickingUpItem.subType) {
            // Record which item we are picking up.
            pickingUpItem.itemType = queuedItem.Type;
            pickingUpItem.subType = queuedItem.ID;
            this.preItemPickup.fire(player, pickingUpItem);
        }
    }
}
exports.ItemPickupDetection = ItemPickupDetection;
