import { Entity, EntityEquippableComponent, EntityInventoryComponent, EntityTypeFamilyComponent, ItemStack } from "@minecraft/server";

export class EntityUtils {
        /** 
     * @param {Entity} entity
     * @returns {ItemStack} 
     */
        static getHandItem(entity) {
            /** @type {EntityEquippableComponent} */
            const component = entity.getComponent(EntityEquippableComponent.componentId);

            return component.getEquipment(EquipmentSlot.Mainhand) ?? new ItemStack("minecraft:air", 1);
        }
    
        /**
         * @param {Entity} entity 
         */
        static hasFamily(entity, family) {
            const familyComponent = entity.getComponent(EntityTypeFamilyComponent.componentId);

            if(familyComponent) {
                return familyComponent.hasTypeFamily(family);
            }

            return false;
        }
    
        static hasItems(entity, id, count = 1) {
            /** @type {EntityInventoryComponent} */
            const inventory = entity.getComponent(EntityInventoryComponent.componentId);

            let amount = count;
    
            for (let i = 0; i < inventory.inventorySize; i++) {
                const item = inventory.container.getItem(i);

                if (item && item.typeId == id) {
                    amount -= item.amount;

                    if (amount <= 0) return true;
                }
            }
    
            return amount <= 0;
        }
}