import { replaySessions } from "../../data/replay-player-session";
import { resetCamSetup } from "../camera/reset-camera-setup";
import { clearStructure } from "../clear-structure";
import { loadBlocksUpToTick } from "../load-blocks-upto-tick";
import { removeEntities } from "../remove-entities";
import { resetRec } from "./reset-replay";
import { cleanupReplayEntities } from "../../multiplayer/cleanup-replay-entities";
export async function doSaveReset(player) {
    const session = replaySessions.playerSessions.get(player.id);
    if (!session) {
        player.sendMessage(`§c[ReplayCraft] Error: No replay session found for you.`);
        return;
    }
    if (session.isReplayActive === true) {
        if (session.textPrompt) {
            player.sendMessage({
                rawtext: [{ translate: "rc1.mes.please.wait.for.replay.or.preview.to.be.completed" }],
            });
        }
        if (session.soundCue) {
            player.playSound("note.bass");
        }
        return;
    }
    resetCamSetup(player);
    session.replayStateMachine.setState("default");
    // Wait for `clearStructure()` to finish before proceeding
    await clearStructure(player, session);
    // Then remove entities
    removeEntities(player, false);
    //Remove offline playback Entities
    cleanupReplayEntities(session);
    // Now safely load blocks
    await loadBlocksUpToTick(session.recordingEndTick, player);
    // Final reset
    resetRec(player);
}
