"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlipDetection = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const players_1 = require("../../../functions/players");
const Feature_1 = require("../../private/Feature");
const v = {
    run: {
        /** We don't consider the case of a multiplayer game with more than one Tainted Lazarus. */
        usedFlipAtLeastOnce: false,
    },
};
class FlipDetection extends Feature_1.Feature {
    v = v;
    postFlip;
    postFirstFlip;
    constructor(postFlip, postFirstFlip) {
        super();
        this.callbacksUsed = [
            // 3
            [isaac_typescript_definitions_1.ModCallback.POST_USE_ITEM, this.postUseItemFlip, [isaac_typescript_definitions_1.CollectibleType.FLIP]],
        ];
        this.postFlip = postFlip;
        this.postFirstFlip = postFirstFlip;
    }
    // ModCallback.POST_USE_ITEM (3)
    // CollectibleType.FLIP (711)
    postUseItemFlip = (_collectibleType, _rng, player, _useFlags, _activeSlot, _customVarData) => {
        if (!(0, players_1.isTaintedLazarus)(player)) {
            return undefined;
        }
        // The player passed as part of the callback will be the old Lazarus that used the Flip item. We
        // pass the new Lazarus to the callback subscribers.
        const newLazarus = getNewLazarus(player);
        if (newLazarus === undefined) {
            return undefined;
        }
        if (!v.run.usedFlipAtLeastOnce) {
            v.run.usedFlipAtLeastOnce = true;
            this.postFirstFlip.fire(newLazarus, player);
        }
        this.postFlip.fire(newLazarus, player);
        return undefined;
    };
}
exports.FlipDetection = FlipDetection;
function getNewLazarus(oldLazarus) {
    const oldCharacter = oldLazarus.GetPlayerType();
    let newCharacter;
    if (oldCharacter === isaac_typescript_definitions_1.PlayerType.LAZARUS_B) {
        newCharacter = isaac_typescript_definitions_1.PlayerType.LAZARUS_2_B;
    }
    else if (oldCharacter === isaac_typescript_definitions_1.PlayerType.LAZARUS_2_B) {
        newCharacter = isaac_typescript_definitions_1.PlayerType.LAZARUS_B;
    }
    else {
        return undefined;
    }
    const playersOfType = (0, players_1.getPlayersOfType)(newCharacter);
    return playersOfType.find((player) => player.FrameCount === oldLazarus.FrameCount);
}
