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

const Flashlight = {
    onUse(event) {
        const player = event.source;
        const flashlightOn = "lv_zo:flashlight_on";
        const flashlightOff = "lv_zo:flashlight_off";

        const equippable = player.getComponent("equippable");
        const heldItem = equippable?.getEquipment(EquipmentSlot.Mainhand);

        if (!heldItem) return;

        player.playSound("lv_zo.flashlight.toggle", player.location);

        if (heldItem.typeId === flashlightOn) {
            equippable.setEquipment(EquipmentSlot.Mainhand, new ItemStack(flashlightOff, 1));
        } else if (heldItem.typeId === flashlightOff) {
            equippable.setEquipment(EquipmentSlot.Mainhand, new ItemStack(flashlightOn, 1));
        }
    }
};


export default Flashlight;
