import {world} from '@minecraft/server';
import {invItemEffects,armourEffects,mainHandItemEffects,offHandItemEffects} from '../config.js';
import {removeItemEffects} from '../utils.js';

world.afterEvents.effectAdd.subscribe(event => {
	const {effect,entity: player} = event;
	if (effect && (effect.typeId == 'mining_fatigue' || effect.typeId == 'weakness') && player.hasTag('btr:cooldown')) return;
	const inv = player.getComponent('inventory').container;
	const head = player.getComponent('equippable').getEquipment('Head');
	const chest = player.getComponent('equippable').getEquipment('Chest');
	const legs = player.getComponent('equippable').getEquipment('Legs');
	const boots = player.getComponent('equippable').getEquipment('Feet');
	const mainHand = player.getComponent('equippable').getEquipment('Mainhand');
	const offHand = player.getComponent('equippable').getEquipment('Offhand');

	if (head && armourEffects[head.typeId]) {
		const armourItem = armourEffects[head.typeId];
		removeItemEffects(armourItem, player, event);
	}
	if (chest && armourEffects[chest.typeId]) {
		const armourItem = armourEffects[chest.typeId];
		removeItemEffects(armourItem, player, event);
	}
	if (legs && armourEffects[legs.typeId]) {
		const armourItem = armourEffects[legs.typeId];
		removeItemEffects(armourItem, player, event);
	}
	if (boots && armourEffects[boots.typeId]) {
		const armourItem = armourEffects[boots.typeId];
		removeItemEffects(armourItem, player, event);
	}
	if (mainHand && mainHandItemEffects[mainHand.typeId]) {
		const mainHandItem = mainHandItemEffects[mainHand.typeId];
		removeItemEffects(mainHandItem, player, event);
	}
	if (offHand && offHandItemEffects[offHand.typeId]) {
		const offHandItem = offHandItemEffects[offHand.typeId];
		removeItemEffects(offHandItem, player, event);
	}

	for (let i = 9; i < inv.size; i++) {
		const item = inv.getItem(i);
		if (!item) continue;
		const typeId = item.typeId;
		const items = invItemEffects[typeId];
		if (!items) continue;
		removeItemEffects(items, player, event);
	}
}, {entityTypes: ['minecraft:player']});
