import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { HeartAbilityManager } from "../systems/index";
const anvilCount = 15;
export async function anvilAbility(player) {
    utils.mclog(`anvil`);
    HeartAbilityManager.addInvulnerability(player, 'noAnvilDamage');
    mc.world.playSound('ability.anvil.activate', player.location);
    let anvilRemaining = anvilCount;
    await utils.waitForLoopEvent(2, () => {
        const anvil = player.dimension.spawnEntity('ldz:anvil_ability', { x: player.location.x + utils.getRandomInteger(-5, 5), y: player.location.y + 10, z: player.location.z + utils.getRandomInteger(-5, 5) });
        anvilFall(anvil, player.location.y + 2);
        mc.world.playSound('custom.pop', anvil.location);
        anvilRemaining--;
        return anvilRemaining < 0;
    });
    utils.waitForEvent(120, () => {
        utils.mclog('Finished');
        HeartAbilityManager.removeInvulnerability(player, 'noAnvilDamage');
    });
}
async function anvilFall(anvil, target_height) {
    await utils.waitForLoopEvent(0, () => anvil.location.y <= target_height);
    if (anvil.isValid()) {
        anvil.triggerEvent('add_collision');
    }
    await utils.waitForLoopEvent(0, () => anvil.hasTag('onGround'));
    await utils.waitForEvent(3, () => { });
    if (anvil.isValid()) {
        mc.world.playSound('ability.anvil.land', anvil.location);
        utils.runCommand(`particle ldz:block_break ${new utils.Vector3(anvil.location).toCommandString()}`);
        anvil.triggerEvent('despawn');
    }
}
