import {modifyStamina} from "../events/stamina"

import { PlatformType, world } from "@minecraft/server";

world.afterEvents.itemCompleteUse.subscribe((event) => {

    const { itemStack, source } = event;
    if (!source || !itemStack) return;
    if (itemStack.typeId === "msft:big_stamina_potion") {
        const inventory = source.getComponent("minecraft:inventory")?.container;
        if (!inventory) return;


        let slotItem = inventory.getItem(source.selectedSlotIndex);
        if (slotItem && slotItem.typeId === itemStack.typeId) {
            if (slotItem.amount > 1) {
                slotItem.amount -= 1; 
                inventory.setItem(source.selectedSlotIndex, slotItem);
            } else {
                inventory.setItem(source.selectedSlotIndex, undefined); 
            }
        }

        const MAX_STAMINA = source.getDynamicProperty("max_stamina");
        const staminaAdd = 120;

        let location = {
            x: source.location.x + 1,
            y: source.location.y + 1.5,
            z: source.location.z
        }
        source.dimension.spawnParticle("msft_bc:consumable", location);
        
        source.playSound("msft:object:potion");
        if(staminaAdd > MAX_STAMINA) {
   
            modifyStamina(source, MAX_STAMINA, "add");
        } else {
       
            modifyStamina(source, staminaAdd, "add");
        }
    }
});
