import { system, EntityDamageCause } from "@minecraft/server";
import { getLinkedPlayer } from "./Linking";
import { mountsFlying } from "./Flying";
import { projectileIDs } from "../DATA/Config";
import { entityDamageConfig } from "../DATA/DamageConfig";
import { cockatriceGasAttack } from "./CockatriceAttack";
import { shadowPhoenixAttack } from "./SPXAttack";
import { kickAttack } from "./PGSAttack";
import { iceDragonAttack } from "./ICEAttack";
export const lastKickAttackTime = {};
function getRandomMelee(config) {
    const randomIndex = Math.floor(Math.random() * 2);
    return randomIndex === 0 ? config.melee : config.melee2;
}
function mountIsFlying(entity) {
    const mountEntity = mountsFlying[entity.id];
    return mountEntity < 0;
}
function isMountType(typeId, mountType) {
    return typeId.startsWith("kicu_mmad:") && typeId.endsWith(mountType);
}
async function shoot(sourceEntity, log, config) {
    const player = getLinkedPlayer(sourceEntity);
    if (player != null) {
        const { x: x1, y: y1, z: z1 } = player.getViewDirection();
        const multiplier = 10 / Math.sqrt((x1 ^ 2) + (z1 ^ 2));
        const { x: pX, y: pY, z: pZ } = sourceEntity.getHeadLocation();
        const { x: vX, y: vY, z: vZ } = player.getViewDirection();
        const { x: rX, y: rY } = player.getRotation();
        const shotProjectileLocation = {
            x: pX + x1 * multiplier,
            y: pY + (mountIsFlying(sourceEntity) ? config.flyingOffset : config.groundOffset),
            z: pZ + z1 * multiplier,
        };
        await sourceEntity.dimension.runCommandAsync(`summon ${config.projectile} ${shotProjectileLocation.x} ${shotProjectileLocation.y} ${shotProjectileLocation.z} ${rX} ${rY}`);
        const shotProjectile = sourceEntity.dimension.getEntities({
            location: shotProjectileLocation,
            type: config.projectile,
            maxDistance: 0.01,
        })[0];
        const bs = config.velocity;
        shotProjectile.setRotation({ x: rX, y: rY });
        shotProjectile.applyImpulse({
            x: vX * bs,
            y: vY * bs,
            z: vZ * bs,
        });
    }
}
function specialAttackTemplate(config, entity, ...args) {
    if (args[0] === "kickAttackFunction") {
        entity.runCommand(`execute positioned ${entity.location.x} ${entity.location.y} ${entity.location.z} at @s run playanimation @s ${mountsFlying[entity.id] === 0 ? config.noFlyShoot : config.flyShoot}`);
        kickAttack(entity, console.warn, config);
    }
    if (args[0] === "cockatriceGasAttack") {
        cockatriceGasAttack(entity, console.warn);
    }
    if (args[0] === "shadowPhoenixAttack") {
        entity.runCommand(`execute positioned ${entity.location.x} ${entity.location.y} ${entity.location.z} at @s run playanimation @s ${mountsFlying[entity.id] === 0 ? config.noFlyShoot : config.flyShoot}`);
        shadowPhoenixAttack(entity);
    }
}
function getRandomParticleEffect(entity) {
    const damagePegasusConfig = entityDamageConfig[entity.typeId];
    const damageConfigParticles = damagePegasusConfig.pegasus?.particleEffects;
    const totalChance = damageConfigParticles?.reduce((sum, effect) => sum + (typeof effect === "string" ? 1 : effect.chance), 0);
    let random = Math.random() * (totalChance ?? 1.0);
    if (damageConfigParticles) {
        for (const effect of damageConfigParticles) {
            if (typeof effect === "string") {
                random -= 1;
                if (random <= 0) {
                    return effect;
                }
            }
            else {
                random -= effect.chance;
                if (random <= 0) {
                    return effect.particle;
                }
            }
        }
        return damagePegasusConfig.pegasus?.particleEffects[0];
    }
    return damagePegasusConfig.pegasus?.particleEffects[0];
}
export function spawnParticleEffect(entity, location, particleId) {
    const particleEffect = particleId || getRandomParticleEffect(entity);
    entity.dimension.spawnParticle(particleEffect, location);
}
export function playSoundEffect(entity, location) {
    const damageConfig = entityDamageConfig[entity.typeId];
    if (damageConfig) {
        entity.dimension.playSound(damageConfig.pegasus?.soundEffect, {
            x: location.x,
            y: location.y,
            z: location.z,
        }, {
            pitch: damageConfig.pegasus?.soundPitch,
            volume: damageConfig.pegasus?.soundVolume,
        });
    }
}
export function skillFluteAttack(entity, log) {
    const config = entityDamageConfig[entity.typeId];
    if (config.special && config.special.overrideAllAttacks) {
        specialAttackTemplate(config, entity, config.special.functionName, config.special.args || []);
        return;
    }
    if (config) {
        if (config.special) {
            specialAttackTemplate(config, entity, config.special.functionName, config.special.args || []);
        }
        else {
            entity.runCommand(`execute positioned ${entity.location.x} ${entity.location.y} ${entity.location.z} at @s run playanimation @s ${mountsFlying[entity.id] === 0 ? config.noFlyShoot : config.flyShoot}`);
            shoot(entity, log, config);
        }
    }
}
function runMeleeAttack(entity, config) {
    const { x: x1, y: y1, z: z1 } = entity.location;
    const { x: x2, z: z2 } = entity.getViewDirection();
    const yOffset = mountIsFlying(entity) ? config.flyingOffset : config.groundOffset;
    const entities = entity.dimension.getEntities({
        location: {
            x: x1 + x2 * config.meleeMultiplier,
            y: y1 + yOffset,
            z: z1 + z2 * config.meleeMultiplier,
        },
        maxDistance: config.meleeRadius,
    });
    for (const value of entities) {
        if (value.id !== entity.id && value.typeId !== "minecraft:player") {
            value.applyDamage(config.meleeDamage, { cause: EntityDamageCause.blockExplosion });
        }
    }
}
export function attackFluteAttack(entity, log) {
    try {
        const config = entityDamageConfig[entity.typeId];
        if (config) {
            if (entity && isMountType(entity.typeId, "pegasus")) {
                entity.runCommand(`execute positioned ${entity.location.x} ${entity.location.y} ${entity.location.z} at @s run playanimation @s ${getRandomMelee(config)}`);
                system.runTimeout(() => {
                    runMeleeAttack(entity, config);
                }, config.meleeDelay);
            }
            else {
                if (entity && isMountType(entity.typeId, "cockatrice")) {
                    entity.runCommand("playsound sound.kicu_mmad_cockatrice.peck @a[r=8] ~ ~ ~ 1.0 1.0 1.0");
                }
                entity.runCommand(`execute positioned ${entity.location.x} ${entity.location.y} ${entity.location.z} at @s run playanimation @s ${getRandomMelee(config)}`);
                runMeleeAttack(entity, config);
            }
        }
    }
    catch { }
}
export const handleProjectileHit = (event) => {
    try {
        const { projectile } = event;
        if (!projectile || !projectileIDs.includes(projectile.typeId))
            return;
        iceDragonAttack(projectile);
        system.runTimeout(() => {
            if (projectile?.isValid()) {
                try {
                    projectile.remove();
                }
                catch (error) { }
            }
        }, 3 * 20);
    }
    catch (error) { }
};

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