import {world} from "@minecraft/server";

world.afterEvents.itemUse.subscribe(event => {
	const {source: player, itemStack} = event;
	if (!player || !itemStack) return;
	//Handles Custom Enchantments For Items
	const assasinateFunction = itemStack.getDynamicProperty('btr:assasinate') == 2 ? 'function enchantment/stab_assasinate_ii' : itemStack.getDynamicProperty('btr:assasinate') == 3 ? 'function enchantment/stab_assasinate_iii' : 'function enchantment/stab_assasinate_i';
	const slamType = itemStack.getDynamicProperty('btr:bash') == 1 ? 'btr:bash_bash_i' : itemStack.getDynamicProperty('btr:bash') == 2 ? 'btr:bash_bash_ii' : itemStack.getDynamicProperty('btr:bash') == 3 ? 'btr:bash_bash_iii' : 'btr:bash';
	const slamToolType = itemStack.hasTag('minecraft:stone_tier') ? 'stone' : itemStack.hasTag('minecraft:iron_tier') ? 'iron' : itemStack.hasTag('minecraft:diamond_tier') ? 'diamond' : itemStack.hasTag('minecraft:netherite_tier') ? 'netherite' : 'wooden';
	if (itemStack.getComponent('cooldown') && itemStack.getComponent('cooldown').getCooldownTicksRemaining(player) != 0) return; 

//Assasinate enchantment
	if (itemStack.hasTag('minecraft:is_dagger') && itemStack.getDynamicProperty('btr:assasinate')) {
		player.runCommandAsync(assasinateFunction);
		itemStack.durabilityModifier(player, 'Mainhand');
		itemStack.getComponent('cooldown').startCooldown(player);
	}
//Handles Slam type based on Bash enchantment and tool tier
	if (itemStack.hasTag('minecraft:is_hammer')) {
		player.addTag('btr:bash');
		const entity = player.dimension.spawnEntity(slamType, player.location);
		system.runTimeout(()=>{player.removeTag('btr:bash')},20);
		entity.triggerEvent(slamToolType);
		itemStack.durabilityModifier(player, 'Mainhand');
		itemStack.getComponent('cooldown').startCooldown(player);
	}
});
