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

const customComponent = "dss_add:wheat_ball_sushi_interaction";
const state = "dss_add:slices";

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent(customComponent, SlicedBreadInteractionComponent);
});

const SlicedBreadInteractionComponent = {
    onPlayerInteract({ block, player, face, faceLocation }) {
        // Get the current number of slices
        const currentPermutation = block.permutation;
        const currentSlices = currentPermutation.getState("dss_add:slices");
        
        if (currentSlices > 0) {
            // Create a new permutation with one less slice
            const newPermutation = currentPermutation.withState("dss_add:slices", currentSlices - 1);
            block.setPermutation(newPermutation);
            
            // Play eating sound and give player food effects
            player.playSound("random.eat");
            player.addEffect("saturation", 7, { amplifier: 1, showParticles: false });
            player.addEffect("regeneration", 600, { amplifier: 1, showParticles: true });
        }
        if (currentSlices === 0) {
            player.playSound("break.calcite");
            block.dimension.getBlock(block.location).setType("minecraft:air");
            const bowl = new ItemStack("minecraft:bowl", 1);
            player.getComponent("inventory").container.addItem(bowl);
        }
    }
}
