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: "haste", duration: 60, amplifier: 0, itemTypeId: "sunflowerdelight:lilac_tincture" },
        { effect: "levitation", duration: 60, amplifier: 0, itemTypeId: "sunflowerdelight:peony_syrup" },
        { effect: "regeneration", duration: 60, amplifier: 0, itemTypeId: "sunflowerdelight:rose_lemonade" },
                { effect: "speed", duration: 30, amplifier: 0, itemTypeId: "sunflowerdelight:golden_sunflower_seed" },
                        { effect: "regeneration", duration: 5, amplifier: 0, itemTypeId: "sunflowerdelight:blooming_salad" },
                                { effect: "strength", duration: 15, amplifier: 0, itemTypeId: "sunflowerdelight:flavored_milkshake" },
                                        { effect: "night_vision", duration: 15, amplifier: 0, itemTypeId: "sunflowerdelight:glowy_milkshake" },
                                                                        { effect: "strength", duration: 30, amplifier: 0, itemTypeId: "sunflowerdelight:flavored_ice_cream" },
                                        { effect: "night_vision", duration: 30, amplifier: 0, itemTypeId: "sunflowerdelight:glowy_ice_cream" },
                                                                                { effect: "slowness", duration: 5, amplifier: 1, itemTypeId: "sunflowerdelight:taiga_custard" },
    ];
    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);
                })
            }
        }
    }
});
