"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EsauJrDetection = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../../core/cachedClasses");
const players_1 = require("../../../functions/players");
const Feature_1 = require("../../private/Feature");
const v = {
    run: {
        usedEsauJrFrame: null,
        usedEsauJrControllerIndex: null,
        usedEsauJrAtLeastOnce: false,
    },
};
class EsauJrDetection extends Feature_1.Feature {
    v = v;
    postEsauJr;
    postFirstEsauJr;
    constructor(postEsauJr, postFirstEsauJr) {
        super();
        this.callbacksUsed = [
            // 1
            [isaac_typescript_definitions_1.ModCallback.POST_UPDATE, this.postUpdate],
            // 3
            [
                isaac_typescript_definitions_1.ModCallback.POST_USE_ITEM,
                this.postUseItemEsauJr,
                [isaac_typescript_definitions_1.CollectibleType.ESAU_JR],
            ],
        ];
        this.postEsauJr = postEsauJr;
        this.postFirstEsauJr = postFirstEsauJr;
    }
    // ModCallback.POST_UPDATE (1)
    postUpdate = () => {
        const gameFrameCount = cachedClasses_1.game.GetFrameCount();
        // Check to see if it is the frame after the player has used Esau Jr.
        if (v.run.usedEsauJrFrame === null ||
            gameFrameCount < v.run.usedEsauJrFrame + 1) {
            return;
        }
        v.run.usedEsauJrFrame = null;
        // Find the player corresponding to the player who used Esau Jr. a frame ago (via matching the
        // ControllerIndex).
        if (v.run.usedEsauJrControllerIndex === null) {
            return;
        }
        const players = (0, players_1.getPlayersWithControllerIndex)(v.run.usedEsauJrControllerIndex);
        v.run.usedEsauJrControllerIndex = null;
        const player = players[0];
        if (player === undefined) {
            return;
        }
        if (!v.run.usedEsauJrAtLeastOnce) {
            v.run.usedEsauJrAtLeastOnce = true;
            this.postFirstEsauJr.fire(player);
        }
        this.postEsauJr.fire(player);
    };
    // ModCallback.POST_USE_ITEM (3)
    // CollectibleType.ESAU_JR (703)
    postUseItemEsauJr = (_collectibleType, _rng, player, _useFlags, _activeSlot, _customVarData) => {
        const gameFrameCount = cachedClasses_1.game.GetFrameCount();
        // The player only changes to Esau Jr. on the frame after the item is used.
        v.run.usedEsauJrFrame = gameFrameCount + 1;
        v.run.usedEsauJrControllerIndex = player.ControllerIndex;
        return undefined;
    };
}
exports.EsauJrDetection = EsauJrDetection;
