import { system, world } from "@minecraft/server";
import { pokemonUtils } from "../utils/pokemonUtils.js";
import { starterMenu } from "../data/ui/starterMenu.js";
import { Debugger } from "../api/debugger.js";
const debug = Debugger.getInstance();
system.afterEvents.scriptEventReceive.subscribe((eventData) => {
    const { id, message, sourceEntity } = eventData;
    if (id == "rp_pr:clear_slot") {
        const slotIndex = parseInt(message);
        if (!sourceEntity || isNaN(slotIndex))
            return;
        pokemonUtils.deletePokemonFromSlot(sourceEntity, slotIndex - 1);
        sourceEntity.sendMessage(`§aSlot ${slotIndex} Pokémon deleted successfully.`);
    }
    if (id == "rp_pr:select_initial") {
        const player = sourceEntity;
        if (!player)
            return;
        starterMenu(player, 0);
    }
    if (id == "rp_pr:clear_pc") {
        const player = sourceEntity;
        if (!player)
            return;
        const success = pokemonUtils.clearAllPokemonFromPC(player);
        if (success) {
            player.sendMessage("§aAll PC Pokémon were deleted successfully.");
        }
        else {
            player.sendMessage("§cCould not delete PC Pokémon.");
        }
    }
    if (id == "rp_pr:debug_slots") {
        const player = sourceEntity;
        if (!player)
            return;
        [0, 1, 2, 3, 4, 5].forEach((i) => {
            system.runTimeout(() => {
                pokemonUtils.debugSlotData(player, i);
                player.sendMessage(`Debugando slot ${i + 1}...`);
            }, 1 * 20 * i);
        });
    }
    if (id == "rp_pr:debug_slot") {
        const player = sourceEntity;
        if (!player)
            return;
        const slotIndex = parseInt(message);
        if (isNaN(slotIndex) || slotIndex < 0 || slotIndex > 5) {
            player.sendMessage("§cInvalid slot index. Use a value between 1 and 6.");
            return;
        }
        pokemonUtils.debugSlotData(player, slotIndex);
        player.sendMessage(`Debugando slot ${slotIndex + 1}...`);
    }
    if (id == "rp_pr:clear_chat") {
        const player = sourceEntity;
        if (!player)
            return;
        debug.log("Clear Chat.");
        for (let i = 0; i < 100; i++) {
            world.sendMessage(" ");
        }
    }
    if (id == "rp_pr:spawn_vanilla") {
        debug.log("Spawn Vanilla Pokémon.");
        world.setDynamicProperty("rp_pr:spawn_vanilla", true);
    }
    if (id == "rp_pr:spawn_addon") {
        debug.log("Spawn Addon Pokémon.");
        world.setDynamicProperty("rp_pr:spawn_vanilla", false);
    }
});
