import * as server from "@minecraft/server";

server.world.afterEvents.itemCompleteUse.subscribe(result => {

    let itemStack = result.itemStack;

    const badEffects = ["bad_omen", "blindness", "fatal_poison", "hunger", "instant_damage", "mining_fatigue", "nausea", "poison", "slowness", "weakness", "wither", "darkness"]
    const allEffects = server.EffectTypes.getAll().map(effect => effect.getName());
    let items = [
        { effect: "conduit_power", duration: 15, amplifier: 0, itemTypeId: "crabbersdelight:kelp_shake" },
        { effect: "blindness", duration: 15, amplifier: 0, itemTypeId: "crabbersdelight:potion_of_inkiness" },
        { effect: "nausea", duration: 30, amplifier: 0, itemTypeId: "crabbersdelight:sea_pickle_juice" },
        { effect: "water_breathing", duration: 180, amplifier: 0, itemTypeId: "crabbersdelight:sea_pickle_juice" },
        { effect: "poison", duration: 15, amplifier: 0, itemTypeId: "crabbersdelight:pufferfish_slice" },
    ];
    for (let item of items) {
        if (itemStack.typeId === item.itemTypeId) {
            let effect = item.effect;
            let duration = item.duration;
            let amplifier = item.amplifier;

            if (item.weight) {
                let weight = item.weight;
                let random = Math.random() * 100;
                if (random < weight) {
                    result.source.addEffect(effect, duration * 20, { amplifier: amplifier });
                }
            } else {
                if (!item.remove) result.source.addEffect(effect, duration * 20, { amplifier: amplifier });
            }

            if (item.remove) {
                item.effect.forEach(effect => {
                    result.source.removeEffect(effect);
                })
            }
        }
    }
});
