import { GameMode, ItemCooldownComponent, system, world } from "@minecraft/server";
import { damageTool, Effect } from "../utils/my_API";
world.beforeEvents.itemUse.subscribe(event => {
    AmuletManager.handleItemUse(event);
});
class Amulet {
    constructor(id, effect, particles, experienceCost) {
        this.id = id;
        this.effect = effect;
        this.particles = particles;
        this.experienceCost = experienceCost;
    }
    tryApplyEffects(event) {
        const itemStack = event.itemStack;
        if (itemStack.typeId !== this.id) {
            return;
        }
        event.cancel = true;
        system.run(() => {
            const player = event.source;
            const icc = itemStack.getComponent(ItemCooldownComponent.componentId);
            const cooldownTicksRemaining = icc.getCooldownTicksRemaining(player);
            if (cooldownTicksRemaining == 0) {
                if (player.matches({ gameMode: GameMode.creative })) {
                    this.applyEffects(player);
                    icc.startCooldown(player);
                }
                else {
                    if (player.level >= this.experienceCost) {
                        icc.startCooldown(player);
                        this.applyEffects(player);
                        damageTool(player);
                        player.runCommand(`xp -${this.experienceCost}L @s`);
                    }
                    else {
                        player.onScreenDisplay.setActionBar({ rawtext: [{ "translate": "nps_mot:peridot_amulet.unsuccessful_use" }] });
                    }
                }
            }
            else {
                player.onScreenDisplay.setActionBar({ rawtext: [{ "translate": "nps_mot:peridot_amulet.active_cooldown" }] });
                player.runCommand('/playsound note.bass @s ~ ~ ~ 1 0.8');
            }
        });
    }
    applyEffects(player) {
        player.addEffect(this.effect.id, this.effect.duration, { amplifier: this.effect.amplifier, showParticles: this.effect.hideParticles });
        player.runCommand('playsound nps_mot:peridot_amulet @s');
        this.particles.forEach(particle => {
            player.runCommand(`particle ${particle}`);
        });
        player.onScreenDisplay.setActionBar({ rawtext: [{ "translate": "nps_mot:peridot_amulet.successful_use" }] });
    }
}
class AmuletManager {
    static handleItemUse(event) {
        for (let amulet of this.amulets) {
            amulet.tryApplyEffects(event);
        }
    }
}
AmuletManager.duration = 60 * 20 * 33;
AmuletManager.amulets = [
    new Amulet('nps_mot:peridot_amulet', new Effect('haste', AmuletManager.duration, 1, false), ['nps_mot:bfb_black', 'nps_mot:bfb_green'], 5),
    new Amulet('nps_mot:fire_opal_amulet', new Effect('fire_resistance', AmuletManager.duration, 0, false), ['nps_mot:bfb_black', 'nps_mot:bfb_fire'], 5),
    new Amulet('nps_mot:lazurite_amulet', new Effect('regeneration', AmuletManager.duration, 1, false), ['nps_mot:bfb_black', 'nps_mot:bfb_water'], 5),
    new Amulet('nps_mot:benitoite_amulet', new Effect('night_vision', AmuletManager.duration, 0, false), ['nps_mot:bfb_black', 'nps_mot:bfb_ice'], 5),
    new Amulet('nps_mot:spinel_amulet', new Effect('strength', AmuletManager.duration, 1, false), ['nps_mot:bfb_black', 'nps_mot:bfb_blue'], 5),
    new Amulet('nps_mot:garnet_amulet', new Effect('speed', AmuletManager.duration, 1, false), ['nps_mot:bfb_black', 'nps_mot:bfb_pink'], 5),
    new Amulet('nps_mot:bismuthinite_amulet', new Effect('absorption', AmuletManager.duration, 1, false), ['nps_mot:bfb_black', 'nps_mot:bfb_yellow'], 5)
];
