"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreNewLevel = void 0;
const ModCallbackCustom_1 = require("../../enums/ModCallbackCustom");
const sprites_1 = require("../../functions/sprites");
const stage_1 = require("../../functions/stage");
const ReadonlySet_1 = require("../../types/ReadonlySet");
const CustomCallback_1 = require("../private/CustomCallback");
const TRAVELING_TO_NEXT_FLOOR_ANIMATIONS = new ReadonlySet_1.ReadonlySet([
    "Trapdoor",
    "LightTravel",
]);
const v = {
    run: {
        firedOnStage: null,
    },
};
class PreNewLevel extends CustomCallback_1.CustomCallback {
    v = v;
    constructor() {
        super();
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PLAYER_RENDER_REORDERED,
                this.postPlayerRenderReordered,
            ],
        ];
    }
    // ModCallbackCustom.POST_PLAYER_RENDER_REORDERED
    postPlayerRenderReordered = (player) => {
        const effectiveStage = (0, stage_1.getEffectiveStage)();
        if (effectiveStage === v.run.firedOnStage) {
            return;
        }
        const sprite = player.GetSprite();
        const animation = sprite.GetAnimation();
        if (!TRAVELING_TO_NEXT_FLOOR_ANIMATIONS.has(animation)) {
            return;
        }
        // We can't use the `Sprite.IsFinished` method to detect when we are at the end of the animation
        // because the player will stop rendering at that point. Thus, revert to checking for the final
        // frame manually.
        const frame = sprite.GetFrame();
        const finalFrame = (0, sprites_1.getLastFrameOfAnimation)(sprite);
        if (frame === finalFrame) {
            v.run.firedOnStage = effectiveStage;
            this.fire(player);
        }
    };
}
exports.PreNewLevel = PreNewLevel;
