import { ActionFormData } from "@minecraft/server-ui";
import { replaySessions } from "../data/replay-player-session";
import { loadBlocksUpToTick } from "../functions/load-blocks-upto-tick";
import { clearStructure } from "../functions/clear-structure";
import { removeEntities } from "../functions/remove-entities";
export function replayMenuAfterLoad(player) {
    const session = replaySessions.playerSessions.get(player.id);
    if (!session) {
        player.sendMessage(`§c[ReplayCraft] Error: No replay session found for you.`);
        return;
    }
    const form = new ActionFormData().title("rc1.replay.menu.after.load.title").button("rc1.replay.menu.after.load.button1").button("rc1.replay.menu.after.load.button2").button("rc1.replay.menu.after.load.button3");
    form.show(player)
        .then(async (response) => {
        if (response.selection === 0) {
            session.replayStateMachine.setState("recPaused");
            await loadBlocksUpToTick(session.recordingEndTick, player);
            // Wait for `clearStructure()` to finish before proceeding
            await clearStructure(player, session);
            // Then remove entities
            removeEntities(player, false);
            // Now safely load blocks
            await loadBlocksUpToTick(session.recordingEndTick, player);
            player.sendMessage("§f§4[ReplayCraft]§f Replay recording has been set to a paused state you will need to resume it.");
            return;
        }
        if (response.selection === 1) {
            session.replayStateMachine.setState("recSaved");
            player.sendMessage("§f§4[ReplayCraft]§f Replay has been loaded please open the menu.");
            removeEntities(player, false);
            return;
        }
        if (response.selection === 2) {
            session.replayStateMachine.setState("recCamSetup");
            player.sendMessage("§f§4[ReplayCraft]§f Replay has been loaded please open the menu.");
            removeEntities(player, false);
            return;
        }
    })
        .catch((error) => {
        player.sendMessage("Failed to show form: " + error);
        return -1;
    });
}
