"use strict";
// This handles logic for the following callbacks:
// - POST_PEFFECT_UPDATE_REORDERED
// - POST_PLAYER_RENDER_REORDERED
// - POST_PLAYER_UPDATE_REORDERED
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlayerReorderedCallbacks = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const constants_1 = require("../../../core/constants");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const array_1 = require("../../../functions/array");
const playerIndex_1 = require("../../../functions/playerIndex");
const Feature_1 = require("../../private/Feature");
const v = {
    run: {
        postGameStartedFiredOnThisRun: false,
        postPEffectUpdateQueue: [],
        postPlayerUpdateQueue: [],
        postPlayerRenderQueue: [],
    },
};
class PlayerReorderedCallbacks extends Feature_1.Feature {
    v = v;
    postPEffectUpdateReordered;
    postPlayerRenderReordered;
    postPlayerUpdateReordered;
    constructor(postPEffectUpdateReordered, postPlayerRenderReordered, postPlayerUpdateReordered) {
        super();
        this.callbacksUsed = [
            // 4
            // eslint-disable-next-line deprecation/deprecation
            [isaac_typescript_definitions_1.ModCallback.POST_PEFFECT_UPDATE, this.postPEffectUpdate],
            // 31
            // eslint-disable-next-line deprecation/deprecation
            [isaac_typescript_definitions_1.ModCallback.POST_PLAYER_UPDATE, this.postPlayerUpdate],
            // 32
            // eslint-disable-next-line deprecation/deprecation
            [isaac_typescript_definitions_1.ModCallback.POST_PLAYER_RENDER, this.postPlayerRender],
        ];
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_GAME_STARTED_REORDERED_LAST,
                this.postGameStartedReorderedLast,
            ],
        ];
        this.postPEffectUpdateReordered = postPEffectUpdateReordered;
        this.postPlayerRenderReordered = postPlayerRenderReordered;
        this.postPlayerUpdateReordered = postPlayerUpdateReordered;
    }
    // ModCallback.POST_PEFFECT_UPDATE (4)
    postPEffectUpdate = (player) => {
        if (v.run.postGameStartedFiredOnThisRun) {
            this.postPEffectUpdateReordered.fire(player);
        }
        else {
            // Defer callback execution until the `POST_GAME_STARTED` callback fires.
            const playerIndex = (0, playerIndex_1.getPlayerIndex)(player);
            v.run.postPEffectUpdateQueue.push({
                playerIndex,
                renderOffset: constants_1.VectorZero,
            });
        }
    };
    // ModCallback.POST_PLAYER_UPDATE (31)
    postPlayerUpdate = (player) => {
        if (v.run.postGameStartedFiredOnThisRun) {
            this.postPlayerUpdateReordered.fire(player);
        }
        else {
            // Defer callback execution until the `POST_GAME_STARTED` callback fires.
            const playerIndex = (0, playerIndex_1.getPlayerIndex)(player);
            v.run.postPlayerUpdateQueue.push({
                playerIndex,
                renderOffset: constants_1.VectorZero,
            });
        }
    };
    // ModCallback.POST_PLAYER_RENDER (32)
    postPlayerRender = (player, renderOffset) => {
        if (v.run.postGameStartedFiredOnThisRun) {
            this.postPlayerRenderReordered.fire(player, renderOffset);
        }
        else {
            // Defer callback execution until the `POST_GAME_STARTED` callback fires.
            const playerIndex = (0, playerIndex_1.getPlayerIndex)(player);
            v.run.postPlayerRenderQueue.push({ playerIndex, renderOffset });
        }
    };
    // ModCallbackCustom.POST_GAME_STARTED_REORDERED_LAST
    postGameStartedReorderedLast = () => {
        v.run.postGameStartedFiredOnThisRun = true;
        dequeue(v.run.postPEffectUpdateQueue, this.postPEffectUpdateReordered.fire);
        dequeue(v.run.postPlayerUpdateQueue, this.postPlayerUpdateReordered.fire);
        dequeue(v.run.postPlayerRenderQueue, this.postPlayerRenderReordered.fire);
    };
}
exports.PlayerReorderedCallbacks = PlayerReorderedCallbacks;
function dequeue(
// eslint-disable-next-line isaacscript/prefer-readonly-parameter-types
queue, fireFunc) {
    for (const element of queue) {
        const { playerIndex, renderOffset } = element;
        const player = (0, playerIndex_1.getPlayerFromIndex)(playerIndex);
        if (player !== undefined) {
            fireFunc(player, renderOffset);
        }
    }
    (0, array_1.emptyArray)(queue);
}
