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

const ITEM_PUNCH_SOUNDS = {
	'tcs_cwp:demolition_hammer': 'tcs_cwp:demolition_hammer_ground_punch',
	'tcs_cwp:ender_dagger': 'tcs_cwp:ender_dagger_punch',
	'tcs_cwp:inferno_scythe': 'tcs_cwp:inferno_scythe_punch',
	'tcs_cwp:lightning_axe': 'tcs_cwp:lighting_axe_punch',
	'tcs_cwp:vampire_fang': 'tcs_cwp:vampire_fang_punch'
};

export async function onEntityHit(event) {
	const attacker = event.damagingEntity;
	if (!attacker || attacker.typeId !== "minecraft:player") return;
	const heldItem = attacker
		.getComponent("minecraft:equippable")
		?.getEquipment(EquipmentSlot.Mainhand);
	
	if (!heldItem) return;

	const itemId = heldItem.typeId;
	const sound = ITEM_PUNCH_SOUNDS[itemId];
	
	if (!sound) return;
	
	const loc = attacker.location;
	utils.playSound(attacker, `@a[x=${loc.x}, y=${loc.y}, z=${loc.z}, r=7]`, sound, 100, 1, 1);
}

