import { EntityComponentTypes, ItemStack, system, world } from "@minecraft/server";
export function initTalismans() {
    // Wither talisman edge case
    world.beforeEvents.entityRemove.subscribe(result => {
        if (result.removedEntity.typeId === "minecraft:wither") {
            // Check HP
            const healthComp = result.removedEntity.getComponent(EntityComponentTypes.Health);
            if (healthComp.currentValue > 0)
                return;
            // Drop item
            const droppedItem = new ItemStack("oct_db:preservation_talisman", 1);
            const dimension = world.getDimension(result.removedEntity.dimension.id);
            const savedLocation = result.removedEntity.location;
            system.runTimeout(() => { dimension.spawnItem(droppedItem, savedLocation); }, 1);
        }
    });
    world.afterEvents.entityDie.subscribe(result => {
        const eligibleEntitiesArr = [
            // { typeId: "minecraft:wither", drop: "oct_db:preservation_talisman" },
            { typeId: "minecraft:elder_guardian", drop: "oct_db:moisturizing_talisman" },
        ];
        if (!eligibleEntitiesArr.map(val => val.typeId).includes(result.deadEntity.typeId))
            return;
        const droppedItem = new ItemStack(eligibleEntitiesArr.find(val => val.typeId === result.deadEntity.typeId).drop, 1);
        if (result.damageSource.damagingEntity)
            result.damageSource.damagingEntity?.dimension.spawnItem(droppedItem, result.deadEntity.location);
        else if (result.damageSource.damagingProjectile)
            result.damageSource.damagingProjectile.dimension.spawnItem(droppedItem, result.deadEntity.location);
    });
}
