import { EquipmentSlot, MolangVariableMap, system } from '@minecraft/server';
import { startCooldown } from 'utils/startCooldown';
export function dash(componentData) {
    const source = componentData.source;
    const view_direction = source.getViewDirection();
    const molang = new MolangVariableMap();
    molang.setFloat('endorah_x', -view_direction.x);
    molang.setFloat('endorah_y', -view_direction.y);
    molang.setFloat('endorah_z', -view_direction.z);
    const particles = [
        'endorah_mo:spectra_special_1',
        'endorah_mo:spectra_special_2',
        'endorah_mo:spectra_special_3'
    ];
    particles.forEach((particle) => {
        source.dimension.spawnParticle(particle, source.location, molang);
    });
    applyImpulseNew(source, {
        x: view_direction.x,
        y: view_direction.y,
        z: view_direction.z
    });
    source.playSound('endorah.mo.spectra.sword_dash');
    const durability_component = componentData.itemStack.getComponent('durability');
    if (durability_component)
        durability_component.damage += 1;
    const equippable_component = source.getComponent('equippable');
    if (equippable_component)
        equippable_component.setEquipment(EquipmentSlot.Mainhand, componentData.itemStack);
    startCooldown(componentData.itemStack, source);
    void system.waitTicks(10).then(() => {
        source.addEffect('minecraft:slow_falling', 20, {
            showParticles: false
        });
    });
}
export function spectraParticleFunction(componentData) {
    try {
        const particles = [
            'endorah_mo:spectra_1',
            'endorah_mo:spectra_2',
            'endorah_mo:spectra_3'
        ];
        particles.forEach((particle) => {
            componentData.hitEntity.dimension.spawnParticle(particle, componentData.hitEntity.location);
        });
    }
    catch (error) { }
}
function applyImpulseNew(entity, vector) {
    const { x, y, z } = vector;
    const previousVelocity = entity.getVelocity();
    // Calculate the norm (magnitude) of the horizontal components (x and z)
    const horizontalNorm = Math.sqrt(x * x + z * z);
    // Calculate directionX and directionZ as normalized values
    let directionX = 0;
    let directionZ = 0;
    if (horizontalNorm !== 0) {
        directionX = x / horizontalNorm;
        directionZ = z / horizontalNorm;
    }
    // The horizontalStrength is the horizontal norm of the input vector
    // multiplied by 2.5 based on experimentation
    const horizontalStrength = horizontalNorm * 2.5;
    // The vertical component is directly taken as verticalStrength
    // The previous velocity is also taken into account, because normal impulse retains
    // the previous velocity and knockback does not
    const verticalStrength = y + previousVelocity.y * 0.9;
    // Apply the knockback
    entity.applyKnockback(directionX, directionZ, horizontalStrength, verticalStrength);
}
