"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostTrinketBreak = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const ModCallbackCustom_1 = require("../../enums/ModCallbackCustom");
const playerDataStructures_1 = require("../../functions/playerDataStructures");
const shouldFire_1 = require("../../shouldFire");
const DefaultMap_1 = require("../DefaultMap");
const CustomCallback_1 = require("../private/CustomCallback");
const TRINKETS_THAT_CAN_BREAK = [
    isaac_typescript_definitions_1.TrinketType.WISH_BONE,
    isaac_typescript_definitions_1.TrinketType.WALNUT,
];
const v = {
    run: {
        // We cannot use a nested `DefaultMap` here.
        playersTrinketMap: new DefaultMap_1.DefaultMap(() => new Map()),
    },
};
class PostTrinketBreak extends CustomCallback_1.CustomCallback {
    v = v;
    constructor() {
        super();
        this.customCallbacksUsed = [
            [ModCallbackCustom_1.ModCallbackCustom.ENTITY_TAKE_DMG_PLAYER, this.entityTakeDmgPlayer],
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
    }
    shouldFire = shouldFire_1.shouldFireTrinketType;
    // ModCallbackCustom.ENTITY_TAKE_DMG_PLAYER
    entityTakeDmgPlayer = (player, _amount, _damageFlags, _source, _countdownFrames) => {
        const trinketMap = (0, playerDataStructures_1.defaultMapGetPlayer)(v.run.playersTrinketMap, player);
        for (const trinketType of TRINKETS_THAT_CAN_BREAK) {
            const numTrinketsHeld = player.GetTrinketMultiplier(trinketType);
            let oldNumTrinketsHeld = trinketMap.get(trinketType);
            if (oldNumTrinketsHeld === undefined) {
                oldNumTrinketsHeld = 0;
            }
            if (numTrinketsHeld >= oldNumTrinketsHeld) {
                continue;
            }
            trinketMap.set(trinketType, numTrinketsHeld);
            // Ensure that the trinket was not dropped on the ground.
            const numTrinketsOnGround = Isaac.CountEntities(undefined, isaac_typescript_definitions_1.EntityType.PICKUP, isaac_typescript_definitions_1.PickupVariant.TRINKET, trinketType);
            if (numTrinketsOnGround > 0) {
                continue;
            }
            this.fire(player, trinketType);
        }
        return undefined;
    };
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        // On every frame, keep track of how many trinkets we have.
        const trinketMap = (0, playerDataStructures_1.defaultMapGetPlayer)(v.run.playersTrinketMap, player);
        for (const trinketType of TRINKETS_THAT_CAN_BREAK) {
            const numTrinkets = player.GetTrinketMultiplier(trinketType);
            if (numTrinkets === 0) {
                trinketMap.delete(trinketType);
            }
            else {
                trinketMap.set(trinketType, numTrinkets);
            }
        }
    };
}
exports.PostTrinketBreak = PostTrinketBreak;
