import { Block, BlockPermutation, Dimension } from "@minecraft/server"

/**
 * Evento del componente buddingChargedAmethystComponent
 * @param {{previousBlock: BlockPermutation, dimension: Dimension, block: Block}} event 
 */
export const buddingChargedAmethystEvent = ({previousBlock, dimension, block}) => {
    try {
        if(!block) return;

        const actions = [
            { weight: 8, commands: [`fill ${block.x} ${block.y+1} ${block.z} ${block.x} ${block.y+1} ${block.z} nps_ec:charged_amethyst_crystal_up replace air`] },
            { weight: 8, commands: [`fill ${block.x} ${block.y-1} ${block.z} ${block.x} ${block.y-1} ${block.z} nps_ec:charged_amethyst_crystal_down replace air`] },
            { weight: 8, commands: [`fill ${block.x} ${block.y} ${block.z+1} ${block.x} ${block.y} ${block.z+1} nps_ec:charged_amethyst_crystal_south replace air`] },
            { weight: 8, commands: [`fill ${block.x} ${block.y} ${block.z-1} ${block.x} ${block.y} ${block.z-1} nps_ec:charged_amethyst_crystal_north replace air`] },
            { weight: 8, commands: [`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${block.z-1} nps_ec:charged_amethyst_crystal_north replace air`] },
            { weight: 8, commands: [`fill ${block.x+1} ${block.y} ${block.z} ${block.x+1} ${block.y} ${block.z} nps_ec:charged_amethyst_crystal_east replace air`] },
            { weight: 8, commands: [`fill ${block.x-1} ${block.y} ${block.z} ${block.x-1} ${block.y} ${block.z} nps_ec:charged_amethyst_crystal_west replace air`] },
            { weight: 52, commands: [] }
        ];

        // Calculate the total weight
        const totalWeight = actions.reduce((acc, action) => acc + action.weight, 0);
        let random = Math.random() * totalWeight;

        for (const action of actions) {
            if (random < action.weight) {
                action.commands.forEach(command => dimension.runCommand(command));
                break;
            }
            random -= action.weight;
        }

    } catch{}
}