import { system, EntityComponentTypes, EntityDamageCause, } from "@minecraft/server";
import { entityDamageConfig } from "../DATA/DamageConfig";
const iceDragonProjectiles = ["kicu_mmad:ice_dragon_projectile", "kicu_mmad:spectral_ice_dragon_projectile"];
function removeInvalidIceEntity(dimension, removedEntityId) {
    const iceBlocks = dimension.getEntities({ type: "kicu_mmad:ice_block" });
    for (const iceBlock of iceBlocks) {
        if (iceBlock.getDynamicProperty("kicu_mmad:removed_entity_id") === removedEntityId) {
            iceBlock.runCommand("event entity @s kicu_mmad:despawns");
        }
    }
}
export function iceDragonAttack(projectile) {
    try {
        if (iceDragonProjectiles.includes(projectile.typeId)) {
            const particleType = "kicu_mmad:freeze";
            const dimension = projectile.dimension;
            const location = projectile.location;
            dimension.spawnParticle(particleType, location);
            const iceDragonConfig = entityDamageConfig[projectile.typeId];
            if (iceDragonConfig) {
                const nearbyTargets = [
                    ...dimension.getEntities({ maxDistance: iceDragonConfig.skillAttackRadius, location, families: ["hostile"] }),
                    ...dimension.getEntities({ maxDistance: iceDragonConfig.skillAttackRadius, location, families: ["mob"] }),
                ];
                for (const target of nearbyTargets) {
                    if (!target || !target.isValid())
                        continue;
                    target.applyDamage(iceDragonConfig.skillDamage, { cause: EntityDamageCause.freezing });
                    const targetId = target.id;
                    const hitEntityLocation = { ...target.location };
                    let tickCounter = 0;
                    target.clearVelocity();
                    const entityHealthComponent = target.getComponent(EntityComponentTypes.Health);
                    if (!target.getDynamicProperty("kicu_mmad:has_ice_block")) {
                        target.setDynamicProperty("kicu_mmad:has_ice_block", true);
                    }
                    else {
                        return;
                    }
                    target.addEffect("slowness", 100, { amplifier: 255, showParticles: false });
                    const iceBlock = dimension.spawnEntity("kicu_mmad:ice_block", hitEntityLocation);
                    iceBlock.setDynamicProperty("kicu_mmad:removed_entity_id", targetId);
                    if (iceDragonConfig && iceDragonConfig.skillDamage > 0) {
                        const intervalId = system.runInterval(() => {
                            if (!target || !target.isValid()) {
                                removeInvalidIceEntity(dimension, targetId);
                                system.clearRun(intervalId);
                                return;
                            }
                            if (entityHealthComponent.currentValue < 1) {
                                removeInvalidIceEntity(dimension, targetId);
                                system.clearRun(intervalId);
                                return;
                            }
                            if (iceDragonConfig.freezeDamageChancePerTick &&
                                Math.random() < iceDragonConfig.freezeDamageChancePerTick) {
                                target.applyDamage(iceDragonConfig.freezeDamage, { cause: EntityDamageCause.freezing });
                            }
                            tickCounter++;
                            if (tickCounter < 5 * 20) {
                                target.teleport(hitEntityLocation);
                            }
                            else {
                                system.clearRun(intervalId);
                                target.setDynamicProperty("kicu_mmad:has_ice_block", undefined);
                            }
                        }, 1);
                    }
                    else {
                        const intervalId = system.runInterval(() => {
                            if (!target || !target.isValid()) {
                                removeInvalidIceEntity(dimension, targetId);
                                system.clearRun(intervalId);
                                return;
                            }
                            if (entityHealthComponent.currentValue < 1) {
                                removeInvalidIceEntity(dimension, targetId);
                                system.clearRun(intervalId);
                                return;
                            }
                            tickCounter++;
                            if (tickCounter < 5 * 20) {
                                target.teleport(hitEntityLocation);
                            }
                            else {
                                system.clearRun(intervalId);
                                target.setDynamicProperty("kicu_mmad:has_ice_block", undefined);
                            }
                        }, 1);
                    }
                }
            }
        }
    }
    catch { }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/ICEAttack.js.map
