import * as mc from "@minecraft/server";
import { Projectile, SafetyCheckedEntity, SafetyCheckedPlayer, Vector3, clamp, mclog, mcwarn } from "../utils/index";
const LOG_TAG = "logBreezeVortex";
const KNOCKBACK_STRENGTH = 3;
const LIFETIME_TICKS = 40;
export function initBreezeVortex() {
    mc.system.afterEvents.scriptEventReceive.subscribe(event => {
        if (event.id === "ldz:breeze_weapon" && event.sourceEntity && event.sourceEntity.typeId === "minecraft:player") {
            const player = new SafetyCheckedPlayer(event.sourceEntity);
            const vortex = new Projectile(player.location, player.dimension, "ldz:breeze_vortex", player.getViewDirection(), player, {
                gravity: 0,
                power: 1.3,
                inertia: 0.95,
                lifetime_seconds: LIFETIME_TICKS / mc.TicksPerSecond,
                offset: { x: 0, y: 1.0, z: 0 },
                shoot_sound: "mob.breeze_vortex.shoot",
                hit_sound: "",
                on_hit: {
                    damage: 0,
                    multiple_targets: true,
                    hit_callback: (projectile, hitEntities, hitBlock) => {
                        if (hitBlock) {
                            projectile.Entity.runCommand("fill ~-2 ~-2 ~-2 ~2 ~2 ~2 air replace fire");
                            projectile.Entity.runCommand("fill ~-2 ~-2 ~-2 ~2 ~2 ~2 air replace soul_fire");
                            projectile.Entity.runCommand("fill ~-2 ~-2 ~-2 ~2 ~2 ~2 campfire[\"extinguished\"=true] replace campfire");
                            projectile.Entity.runCommand("fill ~-2 ~-2 ~-2 ~2 ~2 ~2 soul_campfire[\"extinguished\"=true] replace soul_campfire");
                            onHitBlock(projectile.Entity);
                        }
                    }
                }
            });
            mc.system.runTimeout(() => {
                if (!vortex.Entity.isValid())
                    return;
                vortex.Entity.dimension.spawnParticle("ldz:breeze_weapon_projectile_burst", vortex.Entity.location);
            }, Math.max(0, LIFETIME_TICKS - 1));
            const knockbackThread = mc.system.runInterval(() => {
                if (!vortex.Entity.isValid()) {
                    mc.system.clearRun(knockbackThread);
                    return;
                }
                const entitiesToThrow = vortex.Entity.dimension
                    .getEntities({
                    location: vortex.Entity.location,
                    maxDistance: 3,
                    excludeTypes: ["ldz:breeze_vortex", "minecraft:item", "minecraft:xp_orb"],
                    excludeTags: ["breezeVortex_isBeingThrown"]
                })
                    .map(entity => new SafetyCheckedEntity(entity))
                    .filter(entity => entity.id !== player.id);
                entitiesToThrow.forEach(entity => {
                    mclog(`Throwing ${entity.typeId}`, LOG_TAG);
                    entity.applyDamage(3, { cause: mc.EntityDamageCause.entityAttack, damagingEntity: player.UnsafeEntity, damagingProjectile: vortex.Entity.UnsafeEntity });
                    const direction = vortex.Entity.getViewDirection();
                    const directionX = direction.x;
                    const directionZ = direction.z;
                    const horizontalStrength = direction.multiply({ x: 1, y: 0, z: 1 }).distance(new Vector3(0)) * KNOCKBACK_STRENGTH;
                    const verticalStrength = clamp(direction.y, 0.1, 100) * KNOCKBACK_STRENGTH;
                    try {
                        entity.applyKnockback(directionX, directionZ, horizontalStrength, verticalStrength);
                    }
                    catch (error) {
                        mcwarn(`Could not apply knockback to ${entity.typeId}`);
                    }
                    entity.dimension.spawnParticle("ldz:breeze_weapon_projectile_burst", entity.location);
                    entityThrowCooldown(entity);
                });
            }, 2);
            const movementThread = mc.system.runInterval(() => {
                if (!vortex.Entity.isValid()) {
                    mc.system.clearRun(movementThread);
                    return;
                }
                vortex.Entity.runCommand("fill ~-1 ~-1 ~-1 ~1 ~1 ~1 air replace fire");
                vortex.Entity.runCommand("fill ~-1 ~-1 ~-1 ~1 ~1 ~1 air replace soul_fire");
                vortex.Entity.runCommand("fill ~-1 ~-1 ~-1 ~1 ~1 ~1 campfire[\"extinguished\"=true] replace campfire");
                vortex.Entity.runCommand("fill ~-1 ~-1 ~-1 ~1 ~1 ~1 soul_campfire[\"extinguished\"=true] replace soul_campfire");
                const block = vortex.Entity.dimension.getBlock(vortex.Entity.location);
                if (block && block.isLiquid) {
                    onHitBlock(vortex.Entity);
                }
            });
            mc.system.runTimeout(() => {
                if (!vortex.Entity.isValid())
                    return;
                vortex.Entity.runCommand("playsound mob.breeze_vortex.wind @a ~ ~ ~");
            });
        }
    });
}
function entityThrowCooldown(entity) {
    entity.addTag("breezeVortex_isBeingThrown");
    mc.system.runTimeout(() => {
        if (!entity.isValid())
            return;
        entity.removeTag("breezeVortex_isBeingThrown");
    }, 3);
}
function onHitBlock(vortex) {
    vortex.dimension.spawnParticle("ldz:breeze_weapon_projectile_burst", vortex.location);
    vortex.runCommand(`playsound mob.breeze_vortex.hit_block @a ~ ~ ~`);
    vortex.remove();
}
