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

/**
 * Evento del componente switchStateComponent
 * @param {{player: Player, block: Block, dimension: Dimension}} event 
 */
export const switchStateEvent = ({ player, block, dimension }) => {
    try {
        if (block === undefined) return;

        let permutation = block.permutation;
        if (block.typeId == 'nps_ec:iron_button' && permutation.getState('nps_ec:state')) return;


        if (block.typeId == 'nps_ec:circuit_breaker') dimension.runCommand(`playsound circuit.breaker @a ${block.x} ${block.y} ${block.z} 0.7`);
        if (block.typeId == 'nps_ec:iron_button') dimension.runCommand(`playsound iron.button @a ${block.x} ${block.y} ${block.z} 0.7`);
        permutation = permutation.withState('nps_ec:state', !permutation.getState('nps_ec:state'));


        if (block.typeId == 'nps_ec:iron_button' && !block.permutation.getState('nps_ec:state')) block.setPermutation(permutation);
        if (block.typeId == 'nps_ec:circuit_breaker') block.setPermutation(permutation);
    } catch { }
}