import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
const beeCount = 5;
export async function beesAbility(player) {
    utils.mclog(`bees`);
    mc.world.playSound('mob.bee.hurt', player.location);
    let beeRemaining = beeCount;
    await utils.waitForLoopEvent(1, () => {
        const location = new utils.Vector3({ x: player.location.x + utils.getRandomInteger(-2, 2), y: player.location.y + 2.25, z: player.location.z + utils.getRandomInteger(-2, 2) });
        utils.runCommand(`event entity @s add_bee_ability_swarm_spawner`, player);
        utils.waitForEvent(3, () => {
            utils.runCommand(`tp @e[family=bee_ability_swarm_spawner,c=1,tag=!dontTP] ${location.toCommandString()}`);
            utils.runCommand(`tag @e[family=bee_ability_swarm_spawner] add dontTP`, player);
        });
        beeRemaining--;
        return beeRemaining <= 0;
    });
    const localHurtEvent = hurtEvent;
    mc.world.afterEvents.entityHurt.subscribe(localHurtEvent);
    utils.waitForEvent(120, () => {
        mc.world.afterEvents.entityHurt.unsubscribe(localHurtEvent);
    });
}
function hurtEvent(event) {
    var _a;
    if (event.damageSource.cause === mc.EntityDamageCause.entityAttack && ((_a = event.damageSource.damagingEntity) === null || _a === void 0 ? void 0 : _a.typeId) === 'ldz:bee_ability_swarm') {
        const target = new utils.Vector3(event.damageSource.damagingEntity.location);
        const stack = event.damageSource.damagingEntity.dimension.spawnItem(new mc.ItemStack('minecraft:honey_bottle', 1), target);
        const impulse = new utils.Vector3({ x: 1, y: 0, z: 1 }).rotateByAngle(utils.getRandomInteger(-180, 180)).normalize().multiply(0.1);
        mc.world.playSound('random.pop', target);
        stack.applyImpulse(impulse);
    }
}
