import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { HeartAbilityManager } from "../systems/index";
import { ignoreEntities } from "../shared/constants";
const duration = 5 * 20;
export function fireAbility(player) {
    utils.mclog(`fire`);
    HeartAbilityManager.addInvulnerability(player, 'noDamage');
    utils.waitForEvent(5, () => HeartAbilityManager.removeInvulnerability(player, 'noDamage'));
    utils.waitForEvent(1, () => utils.runCommand(`function abilities/fire/activate`, player));
    const localEvent = (event) => { onImpactEvent(event, player); };
    utils.waitForEvent(5, () => mc.system.afterEvents.scriptEventReceive.subscribe(localEvent));
    utils.waitForEvent(40, () => mc.system.afterEvents.scriptEventReceive.unsubscribe(localEvent));
}
function onImpactEvent(event, player) {
    if (event.id === 'ldz:on_impact' && event.sourceEntity && event.sourceEntity.isValid() && event.sourceEntity.hasTag('onGround')) {
        const dimension = event.sourceEntity.dimension;
        const origin = new utils.Vector3(event.sourceEntity.location).subtract(new utils.Vector3(event.sourceEntity.getViewDirection()).multiply(1));
        const block = dimension.getBlockFromRay(origin, event.sourceEntity.getViewDirection(), { includeLiquidBlocks: false, includePassableBlocks: false, maxDistance: 5 });
        let target = origin;
        if (block) {
            target = new utils.Vector3(block.block.location).add(new utils.Vector3(block.faceLocation));
        }
        event.sourceEntity.triggerEvent('despawn');
        utils.runCommand(`particle ldz:ability_fire_base ${target.toCommandString()}`);
        utils.runCommand(`particle ldz:ability_fire_column ${target.toCommandString()}`);
        utils.runCommand(`particle ldz:ability_fire_sparks ${target.toCommandString()}`);
        utils.runCommand(`particle ldz:ability_fire_core ${target.toCommandString()}`);
        utils.runCommand(`particle ldz:ability_fire_smoke ${target.toCommandString()}`);
        utils.runCommand(`execute unless entity @s[tag=InSpawnHouse] run fill ${target.toCommandString()} ${target.add({ x: 0, y: 1, z: 0 }).toCommandString()} fire [] replace air`, player);
        const end_time = mc.system.currentTick + duration;
        utils.waitForLoopEvent(10, () => {
            dimension.getEntities({ location: target, maxDistance: 4, excludeTypes: ignoreEntities, excludeNames: [player.nameTag], excludeTags: ['friendlyFireOff'] }).forEach(victim => victim.runCommand('summon ldz:igniter ^ ^ ^'));
            mc.world.playSound('fire.fire', target);
            return mc.system.currentTick > end_time;
        });
    }
}
