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

const Utils = {
  
  spawnParticle(player, particle) {
    const loc = player.location;
    try {
      player.dimension.spawnParticle(particle, {
        x: loc.x,
        y: loc.y + 1,
        z: loc.z
      });
    } catch (e) {}
  }
};

const knifeMapping = {
    "peach:knife_cppr_ent": "peach:knife_copper",
    "peach:knife_flnt_ent": "peach:knife_silex",
    "peach:knife_emrld_ent": "peach:knife_emerald"
};

system.runInterval(() => {
    for (const dimension of [world.getDimension("overworld"), world.getDimension("nether"), world.getDimension("the_end")]) {
        try {
            for (const [entityType, itemType] of Object.entries(knifeMapping)) {
                const knives = dimension.getEntities({ type: entityType });
                
                for (const knife of knives) {
                    const velocity = knife.getVelocity();
                    const isOnGround = Math.abs(velocity.x) < 0.1 && Math.abs(velocity.y) < 0.1 && Math.abs(velocity.z) < 0.1;
                    
                    if (isOnGround) {
                        const nearbyPlayers = dimension.getPlayers({
                            location: knife.location,
                            maxDistance: 1.5
                        });
                        
                        if (nearbyPlayers.length > 0) {
                            const player = nearbyPlayers[0];
                            
                            player.playSound("random.pop");
                            
                            if (player.getGameMode() !== "creative") {
                                const inventory = player.getComponent("inventory");
                                if (inventory && inventory.container) {
                                    inventory.container.addItem(new ItemStack(itemType, 1));
                                }
                            }
                            knife.remove();
                        }
                    }
                }
            }
        } catch (e) {}
    }
}, 5);

world.afterEvents.itemCompleteUse.subscribe((event) => {
    const player = event.source;
    const itemStack = event.itemStack;

    if (itemStack.typeId === "peach:bottle_wine" || itemStack.typeId === "peach:mug_wine" || itemStack.typeId === "peach:mug_mead" || itemStack.typeId === "peach:mug_beer") {
        system.runTimeout(() => {
            if (player.isValid()) {
                player.addEffect("nausea", 4800, {
                    amplifier: 3,
                    showParticles: false
                });
            }
        }, 1800);
    }
    if (itemStack.typeId === "peach:bowl_fire_soup") {
        
        player.addEffect("strength", 12000, {
            amplifier: 4,
            showParticles: true
        });
        player.addEffect("hunger", 200, {
            amplifier: 225,
            showParticles: false
        });
    }
    if (itemStack.typeId === "peach:popsicle_charged_amethyst") {
        
        player.addEffect("night_vision", 12000, {
            amplifier: 1,
            showParticles: true
        });
        player.addEffect("haste", 6000, {
            amplifier: 5,
            showParticles: true
        });
    }
    if (itemStack.typeId === "peach:sorbet_coppery") {
        
        player.addEffect("strength", 8000, {
            amplifier: 1,
            showParticles: true
        });
        player.addEffect("regeneration", 8000, {
            amplifier: 2,
            showParticles: true
        });
    }
    if (itemStack.typeId === "peach:pot_turmenic") {
        
        player.addEffect("invisibility", 600, {
            amplifier: 1,
            showParticles: false
        });
        player.addEffect("speed", 600, {
            amplifier: 4,
            showParticles: false
        });
        Utils.spawnParticle(player, "peach:turmenic");
    }
    if (itemStack.typeId === "peach:pot_paprika") {
        
        player.addEffect("invisibility", 600, {
            amplifier: 1,
            showParticles: false
        });
        player.addEffect("jump_boost", 600, {
            amplifier: 4,
            showParticles: false
        });
        Utils.spawnParticle(player, "peach:paprika");
    }
    if (itemStack.typeId === "peach:pot_oregano") {
        
        player.addEffect("invisibility", 600, {
            amplifier: 1,
            showParticles: false
        });
        player.addEffect("night_vision", 600, {
            amplifier: 1,
            showParticles: false
        });
        Utils.spawnParticle(player, "peach:oregano");
    }
    if (itemStack.typeId === "peach:pot_salt") {
        
        player.addEffect("invisibility", 600, {
            amplifier: 1,
            showParticles: false
        });
        player.addEffect("levitation", 20, {
            amplifier: 15,
            showParticles: false
        });
        player.addEffect("slow_falling", 600, {
            amplifier: 1,
            showParticles: false
        });
        Utils.spawnParticle(player, "peach:salt");
        
    }
    if (itemStack.typeId === "peach:pot_black_pepper") {
        
        player.addEffect("invisibility", 600, {
            amplifier: 1,
            showParticles: false
        });
        player.addEffect("strength", 300, {
            amplifier: 10,
            showParticles: false
        });
        Utils.spawnParticle(player, "peach:pepper");
    }
});