import { replaySessions } from "../data/replay-player-session.js";
import { isChunkLoaded } from "./is-chunk-loaded.js";
import { waitForChunkLoad } from "./wait-for-chunk-load.js";
import { replayCraftSkinDB } from "../classes/subscriptions/world-initialize.js";
import { removeEntities } from "./remove-entities.js";
import { debugError, debugLog } from "../data/util/debug.js";
export async function loadEntity(player) {
    const session = replaySessions.playerSessions.get(player.id);
    if (!session) {
        debugError(`No session found for player ${player.name}`);
        return;
    }
    const posData = session.replayPositionDataMap.get(player.id);
    const rotData = session.replayRotationDataMap.get(player.id);
    if (!posData || !rotData || posData.recordedPositions.length === 0) {
        debugError(`Replay data missing for player ${player.name}`);
        return;
    }
    let customEntity;
    const maxIndex = Math.min(session.targetFrameTick, posData.recordedPositions.length - 1);
    const summonPos = posData.recordedPositions[maxIndex];
    // Ensure chunk is loaded
    if (!isChunkLoaded(summonPos, player)) {
        debugLog(`Chunk not loaded for ${player.name}, teleporting...`);
        // Teleport player near the chunk to load it
        const success = player.tryTeleport(summonPos, { checkForBlocks: false });
        if (success) {
            await waitForChunkLoad(summonPos, player);
        }
        else {
            debugError(`Failed to teleport ${player.name} to load chunk at ${summonPos.x}, ${summonPos.y}, ${summonPos.z}`);
            return;
        }
    }
    // Now summon the entity
    try {
        removeEntities(player, true);
        let skinData = replayCraftSkinDB.get(player.id);
        if (!skinData) {
            debugError(`[ReplayCraft] Failed to retrieve skin data for ${player.id}, have they set a skin?`);
            skinData = "0,0";
        }
        const [skinIDStr, modelIDStr] = skinData.split(",");
        let skinID = parseInt(skinIDStr);
        let modelID = parseInt(modelIDStr);
        if (modelID === 0) {
            customEntity = player.dimension.spawnEntity("dbg:replayentity_steve", summonPos);
            customEntity.setRotation(rotData.recordedRotations[maxIndex]);
            customEntity.addTag("owner:" + player.id);
        }
        if (modelID === 1) {
            customEntity = player.dimension.spawnEntity("dbg:replayentity_alex", summonPos);
            customEntity.setRotation(rotData.recordedRotations[maxIndex]);
            customEntity.addTag("owner:" + player.id);
        }
        customEntity.setProperty("dbg:skin", skinID);
        if (session.settingNameType === 0) {
            customEntity.nameTag = player.name;
        }
        else if (session.settingNameType === 1) {
            customEntity.nameTag = player.name;
        }
        else if (session.settingNameType === 2) {
            customEntity.nameTag = session.settingCustomName;
        }
    }
    catch (error) {
        debugError(`Error spawning entity at ${summonPos.x}, ${summonPos.y}, ${summonPos.z}:`, error);
        return;
    }
    customEntity.nameTag = player.name;
}
