import {world,system,BlockPermutation} from '@minecraft/server';
import {skillRequirements,logs} from '../config.js';
import {cancelEvent} from '../utils.js';

world.beforeEvents.playerBreakBlock.subscribe(event => {
	const {block: b,player,itemStack} = event;
	if (itemStack) {
		const typeId = itemStack.typeId;
		const item = skillRequirements['items'][typeId];
		cancelEvent(item, player, event);
	}
	if (b) {
		const typeId = b.typeId;
		const block = skillRequirements['blocks'][typeId];
		cancelEvent(block, player, event, 'block');
	}
	//Handles Custom Enchantments For Items
	const {x,y,z} = b.center();
	const rng = Math.floor(Math.random()*100);
	const lootSpawn = (b.typeId == 'minecraft:gold_ore' || b.typeId == 'minecraft:deepslate_gold_ore') ? 'enchantment/smelt/gold' : (b.typeId == 'minecraft:iron_ore' || b.typeId == 'minecraft:deepslate_iron_ore') ? 'enchantment/smelt/iron' : b.typeId == 'minecraft:ancient_debris' ? 'enchantment/smelt/netherite' : 0;
	const lootSpawn1 = (b.typeId == 'minecraft:gold_ore' || b.typeId == 'minecraft:deepslate_gold_ore') ? 'enchantment/smelt/gold' : (b.typeId == 'minecraft:iron_ore' || b.typeId == 'minecraft:deepslate_iron_ore') ? 'enchantment/smelt/iron' : 0;
	if (!player || (player['cooldown'] ?? 0) > Date.now()) return;
	if (itemStack && itemStack.hasTag('minecraft:is_pickaxe') && itemStack.getDynamicProperty('btr:smelting') && lootSpawn && (itemStack.getComponent('enchantable') && !itemStack.getComponent('enchantable').hasEnchantment('silk_touch'))) {
		system.run(() => {
			b.setPermutation(BlockPermutation.resolve('minecraft:air'));
			player.runCommandAsync(`loot spawn ${x} ${y} ${z} loot "${lootSpawn}"`);
		});
		event.cancel = true;
	}
	if (itemStack && itemStack.hasTag('minecraft:is_pickaxe') && itemStack.typeId == 'lst:blazer_pickaxe' && !itemStack.getDynamicProperty('btr:smelting') && !itemStack.getComponent('enchantable').hasEnchantment('silk_touch') && lootSpawn) {
		system.run(() => {
			b.setPermutation(BlockPermutation.resolve('minecraft:air'));
			b.dimension.spawnEntity('minecraft:xp_orb', block.location);
			player.runCommandAsync(`loot spawn ${x} ${y} ${z} loot "${lootSpawn1}"`);
		});
		event.cancel = true;
	}
	if (itemStack && itemStack.hasTag('minecraft:is_pickaxe') && itemStack.getDynamicProperty('btr:diamondsEverywhere') && (itemStack.getComponent('enchantable') && !itemStack.getComponent('enchantable').hasEnchantment('silk_touch')) && rng <= 15 && (b.typeId == 'minecraft:coal_ore' || b.typeId == 'minecraft:emerald_ore'  || b.typeId == 'minecraft:gold_ore' || b.typeId == 'minecraft:iron_ore' || b.typeId == 'minecraft:deepslate_coal_ore' || b.typeId == 'minecraft:deepslate_emerald_ore'  || b.typeId == 'minecraft:deepslate_gold_ore' || b.typeId == 'minecraft:deepslate_iron_ore')) {
		const lootSpawn = 'enchantment/diamond';
		system.runTimeout(() => {
			player.runCommandAsync(`loot spawn ${x} ${y} ${z} loot "${lootSpawn}"`);
		},2);
	}
	if (itemStack && itemStack.hasTag('minecraft:is_pickaxe') && itemStack.getDynamicProperty('chr:collecting')) {
		system.runTimeout(()=> {
			const entities = player.dimension.getEntities({ type: "minecraft:item", location: player.location, maxDistance: 5, closest: 5 });
			entities.forEach(entity => {
				entity.teleport(player.location);
			});
		}, 5);
	}
	if (player.getGameMode() != 'creative' && logs.includes(b.typeId) && (!itemStack || (itemStack && !itemStack.hasTag('minecraft:is_axe')))) event.cancel = true;
	player['cooldown'] = Date.now() + 500;
});
