import { EquipmentSlot, system } from '@minecraft/server';
import { startCooldown } from 'utils/startCooldown';
import { tan } from './amethyst';
export function lightning(componentData) {
    const source = componentData.source;
    const dimension = source.dimension;
    const blockRaycast = source.getBlockFromViewDirection();
    let base_location;
    if (!blockRaycast) {
        const tan_location = tan(source.getViewDirection());
        const new_location = {
            x: tan_location.x * 32,
            y: tan_location.y * 32,
            z: tan_location.z * 32
        };
        base_location = dimension.getBlock({
            x: source.location.x + new_location.x,
            y: source.location.y + new_location.y,
            z: source.location.z + new_location.z
        });
    }
    else {
        const offset = blockRaycast.faceLocation;
        base_location = blockRaycast.block.offset(offset);
    }
    const locations = [
        source.dimension.getBlock(base_location),
        source.dimension.getBlock(base_location.above(3)),
        source.dimension.getBlock(base_location.above(6))
    ];
    const particle = 'endorah_mo:titanite_special_3';
    const sound = 'ambient.weather.lightning.impact';
    locations.forEach((location) => {
        for (let i = 0; i < 3; i++)
            source.dimension.spawnParticle(particle, location);
    });
    source.dimension.playSound(sound, locations[0]);
    source.dimension
        .getEntities({
        location: locations[0],
        maxDistance: 2,
        excludeTypes: ['minecraft:item']
    })
        .forEach((entity) => {
        entity.applyDamage(10);
    });
    const job = system.runInterval(() => {
        locations.forEach((location) => {
            for (let i = 0; i < 3; i++)
                source.dimension.spawnParticle(particle, location);
        });
        source.dimension.playSound(sound, locations[0]);
        source.dimension
            .getEntities({
            location: locations[0],
            maxDistance: 2,
            excludeTypes: ['minecraft:item']
        })
            .forEach((entity) => {
            entity.applyDamage(10);
        });
    }, 10);
    system.runTimeout(() => {
        system.clearRun(job);
    }, 20);
    source.playSound('endorah.mo.titanite.sword_lightning_sound');
    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);
}
export function titaniteParticleFunction(componentData) {
    try {
        const particles = [
            'endorah_mo:titanite_1',
            'endorah_mo:titanite_2',
            'endorah_mo:titanite_3'
        ];
        particles.forEach((particle) => {
            componentData.hitEntity.dimension.spawnParticle(particle, componentData.hitEntity.location);
        });
    }
    catch (error) { }
}
