import * as mc from "@minecraft/server"
const { world, system } = mc;

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

    let itemStack = result.itemStack;

    let items = [
        { effect: "nausea", duration: 480, amplifier: 0, itemTypeId: "brewinandchewin:beer" },
        { effect: "nausea", duration: 720, amplifier: 0, itemTypeId: "brewinandchewin:bloody_mary" },
                { effect: "nausea", duration: 300, amplifier: 2, itemTypeId: "brewinandchewin:dread_nog" },
        { effect: "bad_omen", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:dread_nog" },
                { effect: "nausea", duration: 60, amplifier: 0, itemTypeId: "brewinandchewin:egg_grog" },
        { effect: "absorption", duration: 180, amplifier: 0, itemTypeId: "brewinandchewin:egg_grog" },
                { effect: "nausea", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:glittering_grenadine" },
        { effect: "night_vision", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:glittering_grenadine" },
                { effect: "nausea", duration: 300, amplifier: 2, itemTypeId: "brewinandchewin:kombucha" },
        { effect: "satisfaction", duration: 180, amplifier: 0, itemTypeId: "brewinandchewin:kombucha" },
                { effect: "haste", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:kombucha" },
        { effect: "nausea", duration: 480, amplifier: 0, itemTypeId: "brewinandchewin:mead" },
                { effect: "nausea", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:pale_jane" },
        { effect: "satisfaction", duration: 480, amplifier: 2, itemTypeId: "brewinandchewin:pale_jane" },
                { effect: "nausea", duration: 1080, amplifier: 0, itemTypeId: "brewinandchewin:red_rum" },
        { effect: "nausea", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:rice_wine" },
                { effect: "satisfaction", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:rice_wine" },
        { effect: "nausea", duration: 480, amplifier: 1, itemTypeId: "brewinandchewin:saccharine_rum" },
                { effect: "nausea", duration: 600, amplifier: 1, itemTypeId: "brewinandchewin:salty_folly" },
        { effect: "water_breathing", duration: 480, amplifier: 0, itemTypeId: "brewinandchewin:salty_folly" },
                { effect: "nausea", duration: 600, amplifier: 2, itemTypeId: "brewinandchewin:steel_toe_stout" },
                { effect: "resistance", duration: 600, amplifier: 2, itemTypeId: "brewinandchewin:steel_toe_stout" },
        { effect: "nausea", duration: 720, amplifier: 1, itemTypeId: "brewinandchewin:strongroot_ale" },
                { effect: "resistance", duration: 300, amplifier: 0, itemTypeId: "brewinandchewin:strongroot_ale" },
        { effect: "nausea", duration: 720, amplifier: 0, itemTypeId: "brewinandchewin:vodka" },
                { effect: "nausea", duration: 1200, amplifier: 2, itemTypeId: "brewinandchewin:withering_dross" },
                { effect: "weakness", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:withering_dross" },
        { effect: "wither", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:withering_dross" },
                { effect: "slowness", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:withering_dross" },
        { effect: "blindness", duration: 600, amplifier: 0, itemTypeId: "brewinandchewin:withering_dross" },
        
    ];
    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);
                })
            }
        }
    }
});
