// Lista de efectos disponibles, con id y un nombre corto para el mensaje.
const effects = [
  { id: "minecraft:speed", name: "Speed" },
  { id: "minecraft:haste", name: "Haste" },
  { id: "minecraft:strength", name: "Strength" },
  { id: "minecraft:jump_boost", name: "Jump" },
  { id: "minecraft:regeneration", name: "Regen" },
  { id: "minecraft:resistance", name: "Resist" },
  { id: "minecraft:fire_resistance", name: "FireResist" },
  { id: "minecraft:water_breathing", name: "WaterB" },
  { id: "minecraft:night_vision", name: "NightVis" },
  { id: "minecraft:invisibility", name: "Invis" },
  { id: "minecraft:slowness", name: "Slow" },
  { id: "minecraft:mining_fatigue", name: "Fatigue" },
  { id: "minecraft:blindness", name: "Blind" },
  { id: "minecraft:hunger", name: "Hunger" },
  { id: "minecraft:weakness", name: "Weak" },
  { id: "minecraft:poison", name: "Poison" },
  { id: "minecraft:wither", name: "Wither" }
];

export const event = {
    onConsume(event) {
        const player = event.source;
        if (!player || !player.addEffect) return;

        // Seleccionamos aleatoriamente 1 o 2 efectos.
        const count = Math.floor(Math.random() * 2) + 1;

        // Baraja la lista de efectos (algoritmo Fisher-Yates).
        let shuffled = effects.slice();
        for (let i = shuffled.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
        }
        const selectedEffects = shuffled.slice(0, count);

        // Aplica cada efecto aleatorio al jugador.
        selectedEffects.forEach(effectObj => {
            // Duración entre 100 y 200 ticks (aprox. 5-10 seg).
            const duration = Math.floor(Math.random() * 101) + 100;
            // Amplificador entre 0 y 2.
            const amplifier = Math.floor(Math.random() * 3);
            try {
                player.addEffect(effectObj.id, duration, { amplifier: amplifier });
            } catch (e) {
                console.warn("Error applying effect: " + effectObj.id, e);
            }
        });

        // Construye el mensaje usando los nombres cortos de los efectos aplicados.
        const effectsText = selectedEffects.map(e => e.name).join(", ");
        const message = `§aStardust: ${effectsText}`;
        // Muestra el mensaje en el actionbar.
        player.runCommandAsync(`title @s actionbar ${message}`);
    }
};