import { world, system } from "@minecraft/server";

world.afterEvents.itemUse.subscribe((event) => {
    const player = event.source;
    const item = player.getComponent("inventory").container.getSlot(player.selectedSlotIndex)?.getItem();

    if (item?.typeId === "enchanted_paperpets:pack_item_c") {
        const randomValue = Math.random();

        if (randomValue < 0.50) {
            // Dar 3 stickers aleatorios
            const stickers = [
                "enchanted_paperpets:cloudy_item",
                "enchanted_paperpets:dinaris_item", "enchanted_paperpets:dunes_item",
                "enchanted_paperpets:kino_item", "enchanted_paperpets:lapis_item",
                "enchanted_paperpets:nova_item", "enchanted_paperpets:otto_item", "enchanted_paperpets:shade_item",
                "enchanted_paperpets:wando_item"
            ];
            for (let i = 0; i < 3; i++) {
                const randomSticker = stickers[Math.floor(Math.random() * stickers.length)];
                player.runCommand(`give @s ${randomSticker}`);
            }
        } else {
            // Spawnear 5 mobs aleatorios
            const mobs = ["enchanted_paperpets:zombie", "enchanted_paperpets:spider", "enchanted_paperpets:coco", "enchanted_paperpets:horns", "enchanted_paperpets:poopie", "enchanted_paperpets:spikes"]; // Reemplazar con los mobs reales
            for (let i = 0; i < 5; i++) {
                const randomMob = mobs[Math.floor(Math.random() * mobs.length)];
                player.runCommand(`summon ${randomMob} ~ ~ ~`);
            }
        }
        // Limpiar el item del inventario
        system.run(() => player.runCommand("clear @s enchanted_paperpets:pack_item_c 0 1"));
    }
    if (item?.typeId === "enchanted_paperpets:pack_item_r") {
        const randomValue = Math.random();

        if (randomValue < 0.33) {
            // Spawnear 5 mobs aleatorios
            const mobs = ["enchanted_paperpets:zombie", "enchanted_paperpets:spider", "enchanted_paperpets:coco", "enchanted_paperpets:horns", "enchanted_paperpets:poopie", "enchanted_paperpets:spikes"]; // Reemplazar con los mobs reales
            for (let i = 0; i < 5; i++) {
                const randomMob = mobs[Math.floor(Math.random() * mobs.length)];
                player.runCommand(`summon ${randomMob} ~ ~ ~`);
            }
        } else if (randomValue < 0.66) {
            // Dar 3 stickers aleatorios
            const stickers = [
                "enchanted_paperpets:arcos_item", "enchanted_paperpets:auralis_item", "enchanted_paperpets:auralis_item_f", "enchanted_paperpets:cloudy_item",
                "enchanted_paperpets:dinaris_item", "enchanted_paperpets:dinaris_item_f", "enchanted_paperpets:dunes_item", "enchanted_paperpets:dunes_item_f", "enchanted_paperpets:fruit_spirit_item_f", "enchanted_paperpets:fruit_spirit_item",
                "enchanted_paperpets:jeppo_item", "enchanted_paperpets:jeppo_item_f", "enchanted_paperpets:kino_item", "enchanted_paperpets:kino_item_f", "enchanted_paperpets:lapis_item", "enchanted_paperpets:lapis_item_f", "enchanted_paperpets:lompy_item_f", "enchanted_paperpets:lompy_item",
                "enchanted_paperpets:mira_item", "enchanted_paperpets:nova_item", "enchanted_paperpets:nova_item_f", "enchanted_paperpets:otto_item", "enchanted_paperpets:shade_item",
                "enchanted_paperpets:solara_item", "enchanted_paperpets:wando_item_f", "enchanted_paperpets:wando_item", "enchanted_paperpets:zara_item", "enchanted_paperpets:zara_item_f"
            ];
            for (let i = 0; i < 5; i++) {
                const randomSticker = stickers[Math.floor(Math.random() * stickers.length)];
                player.runCommand(`give @s ${randomSticker}`);
            }
        } else {
            // Dar 3 attachables aleatorios
            const attachables = [
                "enchanted_paperpets:bow", "enchanted_paperpets:chef", "enchanted_paperpets:clownmask", "enchanted_paperpets:crystalwings", "enchanted_paperpets:darkwings",
                "enchanted_paperpets:eyes", "enchanted_paperpets:flower", "enchanted_paperpets:halo", "enchanted_paperpets:hornss", "enchanted_paperpets:lollipop", "enchanted_paperpets:medal",
                "enchanted_paperpets:moustache", "enchanted_paperpets:partyhat", "enchanted_paperpets:red_tail", "enchanted_paperpets:sword",
                "enchanted_paperpets:tail", "enchanted_paperpets:tie", "enchanted_paperpets:tophat", "enchanted_paperpets:unicorn", "enchanted_paperpets:wings"
            ];
            for (let i = 0; i < 3; i++) {
                const randomAttachable = attachables[Math.floor(Math.random() * attachables.length)];
                player.runCommand(`give @s ${randomAttachable}`);
            }
        }
        // Limpiar el item del inventario
        system.run(() => player.runCommand("clear @s enchanted_paperpets:pack_item_r 0 1"));
    }
    if (item?.typeId === "enchanted_paperpets:pack_item_l") {
        const randomValue = Math.random();

        if (randomValue < 0.33) {
            // Spawnear 5 mobs aleatorios
            const mobs = ["enchanted_paperpets:zombie", "enchanted_paperpets:spider", "enchanted_paperpets:coco", "enchanted_paperpets:horns", "enchanted_paperpets:poopie", "enchanted_paperpets:spikes"]; // Reemplazar con los mobs reales
            for (let i = 0; i < 5; i++) {
                const randomMob = mobs[Math.floor(Math.random() * mobs.length)];
                player.runCommand(`summon ${randomMob} ~ ~ ~`);
            }
        } else if (randomValue < 0.66) {
            // Dar 3 stickers aleatorios
            const stickers = [
                "enchanted_paperpets:arcos_item_f", "enchanted_paperpets:arcos_item", "enchanted_paperpets:auralis_item", "enchanted_paperpets:auralis_item_f", "enchanted_paperpets:cloudy_item_f", "enchanted_paperpets:cloudy_item",
                "enchanted_paperpets:dinaris_item", "enchanted_paperpets:dinaris_item_f", "enchanted_paperpets:dunes_item", "enchanted_paperpets:dunes_item_f", "enchanted_paperpets:fruit_spirit_item_f", "enchanted_paperpets:fruit_spirit_item",
                "enchanted_paperpets:jeppo_item", "enchanted_paperpets:jeppo_item_f", "enchanted_paperpets:kino_item", "enchanted_paperpets:kino_item_f", "enchanted_paperpets:lapis_item", "enchanted_paperpets:lapis_item_f", "enchanted_paperpets:lompy_item_f", "enchanted_paperpets:lompy_item",
                "enchanted_paperpets:mira_item", "enchanted_paperpets:mira_item_f", "enchanted_paperpets:nova_item", "enchanted_paperpets:nova_item_f", "enchanted_paperpets:otto_item", "enchanted_paperpets:otto_item_f", "enchanted_paperpets:shade_item_f", "enchanted_paperpets:shade_item",
                "enchanted_paperpets:solara_item", "enchanted_paperpets:solara_item_f", "enchanted_paperpets:wando_item_f", "enchanted_paperpets:wando_item", "enchanted_paperpets:zara_item", "enchanted_paperpets:zara_item_f"
            ];
            for (let i = 0; i < 6; i++) {
                const randomSticker = stickers[Math.floor(Math.random() * stickers.length)];
                player.runCommand(`give @s ${randomSticker}`);
            }
        } else {
            // Dar 3 attachables aleatorios
            const attachables = [
                "enchanted_paperpets:bow", "enchanted_paperpets:chef", "enchanted_paperpets:clownmask", "enchanted_paperpets:crystalwings", "enchanted_paperpets:darkwings",
                "enchanted_paperpets:eyes", "enchanted_paperpets:flower", "enchanted_paperpets:halo", "enchanted_paperpets:hornss", "enchanted_paperpets:lollipop", "enchanted_paperpets:medal",
                "enchanted_paperpets:moustache", "enchanted_paperpets:partyhat", "enchanted_paperpets:red_tail", "enchanted_paperpets:sword",
                "enchanted_paperpets:tail", "enchanted_paperpets:tie", "enchanted_paperpets:tophat", "enchanted_paperpets:unicorn", "enchanted_paperpets:wings"
            ];
            for (let i = 0; i < 3; i++) {
                const randomAttachable = attachables[Math.floor(Math.random() * attachables.length)];
                player.runCommand(`give @s ${randomAttachable}`);
            }
        }

        // Limpiar el item del inventario
        system.run(() => player.runCommand("clear @s enchanted_paperpets:pack_item_l 0 1"));
    }
});
