import { world, system, ItemStack, EquipmentSlot, EnchantmentType } from "@minecraft/server";
import { fo } from "./utilities/fo_constants";


export function initCursedItems() {

    const interval = Math.floor(Math.random() * 12000 + 19200); 

    system.runInterval(() => {
        

        const cursedPlayers = world.getPlayers().filter(player => player.getDynamicProperty("PumpkinCurse") === true);


        
        if (cursedPlayers.length === 0) return; 

        const pumpkinHelmet = "carved_pumpkin";
        
        const cursedPumpkin = new ItemStack(pumpkinHelmet, 1);
        cursedPumpkin.setLore([
            `§5§lᛃᚨᛉ`,
        ]);
        const enchantComponent = cursedPumpkin.getComponent("minecraft:enchantable")
        enchantComponent.addEnchantments([
            { level: 1, type: new EnchantmentType("binding"), },
            { level: 1, type: new EnchantmentType("vanishing"), }
        ]);

        cursedPlayers.forEach(player => {
            
            const equipComponent = player.getComponent("minecraft:equippable");
            const currentHelmet = equipComponent.getEquipment(EquipmentSlot.Head);

            if (currentHelmet && currentHelmet.typeId === cursedPumpkin.typeId) return; 


            
            const inventory = player.getComponent("minecraft:inventory").container;
            for (let i = 0; i < inventory.size; i++) {
                const item = inventory.getItem(i);
                if (item && item.typeId === cursedPumpkin.typeId) return; 

            }

            inventory.addItem(cursedPumpkin);

        }
        )

    }, interval);


};