import { TicksPerSecond } from '@minecraft/server';
import { MCSounds } from '../../constants.js';
import { shrinkItemStack } from '../../utils/utils.js';
import { setEntityHeldItem } from '../../utils/players.js';
import { DeadLeavesRecipes } from '../../recipes/recipes.js';
import { MinecraftEffectTypes } from '../../minecraft_vanilla_data.js';

class GummyLeafWormItemComponent {
    onConsume(event) {
        const source = event.source;
        source.addEffect(MinecraftEffectTypes.Speed, 5 * TicksPerSecond, {
            amplifier: 2,
            showParticles: false
        });
    }
}
class LeafWormItemComponent {
    onUseOn(event) {
        const block = event.block;
        let deadType = getDeadFromLeaf(block.typeId);
        if (!deadType) {
            return;
        }
        block.setType(deadType);
        block.dimension.playSound(MCSounds.COMPOST_FILL, block.location);
        setEntityHeldItem(event.source, shrinkItemStack(event.itemStack, 1));
    }
}
function getDeadFromLeaf(leaf) {
    let deadType;
    for (let leavestoleaf of DeadLeavesRecipes.DEAD_LEAVES_TO_LEAVES) {
        if (leaf === leavestoleaf[1]) {
            deadType = leavestoleaf[0];
            break;
        }
    }
    return deadType;
}

export { GummyLeafWormItemComponent, LeafWormItemComponent };
