import { BlockPermutation } from "@minecraft/server";
import { cakes } from '../config.js';

export class CakeCustomComponent {
    static onInteract(data) {
        const { block, dimension, player } = data;
        if (!player) return;
        const cake = cakes.find((f)=>f.blockID == block.typeId);
        if (!cake) return;
        const state = block.permutation.getState(cake.stateID);
        if (state == undefined) return;
        if (state - 1 >= 0) {
            block.setPermutation(block.permutation.withState(cake.stateID, state - 1));
        } else {
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
        }
        if (cake.effects) {
            for (const effect of cake.effects){
                player.addEffect(effect.id, effect.duration * 20, {
                    amplifier: effect.amplifier,
                    showParticles: effect.showParticles
                });
            }
        }
        if (cake.eatNoise) {
            const sound = cake.eatNoise;
            dimension.playSound(sound.id, block.center(), {
                volume: sound.volume,
                pitch: sound.pitch
            });
        }
        if (cake.onEat) cake.onEat(player);
    }
}
