import * as game from "../game";
import { EquipmentSlot } from "@minecraft/server";
import { damageItem } from "../inventory_utils";
import * as utils from '../utils';

export async function onEntityHurt(event) {
    const attacker = event.damageSource?.damagingEntity;
    if (!attacker || attacker.typeId !== "minecraft:player") return;

    	// Punch animations
	attacker.startItemCooldown("tcs_cwp_lightning_axe_punch", 5);
	attacker.playAnimation("animation.goe_rwp.lightning_axe.tp_attack");

    const target = event.hurtEntity;
    if (!target) return;

    const heldItem = attacker.getComponent("minecraft:equippable")?.getEquipment(EquipmentSlot.Mainhand);
    if (!heldItem || heldItem.typeId !== "tcs_cwp:lightning_axe") return;

    const pos = target.location;
    await game.runCommand(`particle tcs_cwp:lightning ${pos.x} ${pos.y} ${pos.z}`);
    utils.playSound(target, `@a[x=${target.location.x}, y=${target.location.y}, z=${target.location.z}, r=15]`, 'tcs_cwp:lighting_axe_lighting', 100, 1, 1);

    utils.applyDamage(target, 4);
    target.addEffect("slowness", 40, { amplifier: 0, showParticles: true });

    if (Math.random() < 0.8) {
        try { target.setOnFire(3, true); } catch (e) {}
    }

    damageItem(attacker, EquipmentSlot.Mainhand, 1);
}