import { ItemStack } from '@minecraft/server';
import { FSTags, FSItems, ADDON_ID, FSItemTags } from '../../constants.js';
import { damageEntityHeldItem, spawnItemAtBlock, spawnLootAtBlock } from '../../utils/utils.js';
import { MinecraftBlockTypes } from '../../minecraft_vanilla_data.js';

const WORM_CHANCE = 0.25;
class CrookItemComponent {
    onMineBlock(event) {
        const block = event.block;
        const brokenBlock = event.minedBlockPermutation;
        damageEntityHeldItem(event.source, 1, event.itemStack);
        if (!FSTags.LEAVES.includes(brokenBlock.type.id)) {
            return;
        }
        if (Math.random() < WORM_CHANCE) {
            spawnItemAtBlock(block, new ItemStack(FSItems.LEAF_WORM));
        }
    }
}
class CrookCustomBreakHandler {
    spawnLoot(block) {
        spawnLootAtBlock(block, ADDON_ID + "/crook_dirt");
    }
    isValidBlock(block) {
        return block.typeId === MinecraftBlockTypes.GrassBlock || block.typeId === MinecraftBlockTypes.Diorite;
    }
    isValidItem(item) {
        return item?.hasTag(FSItemTags.CROOK);
    }
    damageItem(item, block) {
        return 1;
    }
}

export { CrookCustomBreakHandler, CrookItemComponent };
