import { system, world, ItemStack } from "@minecraft/server";

export function chainsawStun() {
  // Handle entity hits to REVERT charged weapons
  world.afterEvents.entityHitEntity.subscribe((event) => {
    const hitter = event.damagingEntity;
    if (!hitter || hitter.typeId !== "minecraft:player") return;

    const equippable = hitter.getComponent("equippable");
    const heldItem = equippable?.getEquipment("Mainhand");
    if (!heldItem) return;

    const heldTypeId = heldItem.typeId;
    if (heldTypeId != "hb_hr:chainsaw") return;

    if (event.hitEntity.isValid()) {
      event.hitEntity.addEffect("slowness", 60, { amplifier: 255, showParticles: false });
      system.runTimeout(() => {
        try {
          hitter.spawnParticle("hb_hr:chainsaw_stun", event.hitEntity.getHeadLocation());
        } catch (error) {}
      }, 5);
    }
  });
}
