import { Block, Dimension } from "@minecraft/server";
import { blockNeighbors } from "../../library/blockManager";

/**
 * Evento del componente cableSignalComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const cableConnectionUpEvent = ({ block, dimension }) => {
    try{
        if(!block) return;
        const {
            blockNeighborUp, blockNeighborDown,
        } = blockNeighbors(block);
    
        if(!blockNeighborUp || !blockNeighborDown) return;
    
        let permutation = block.permutation;

        if(block.typeId === 'minecraft:air') return
        
        if(block.typeId === 'nps_ec:farmer') {
            const conditions = (
                    blockNeighborUp.hasTag('cable') || 
                    blockNeighborUp.hasTag('cable:up:down')
                )
            permutation = permutation.withState('nps_ec:down', conditions);
        }

        permutation = permutation.withState('nps_ec:up', (blockNeighborUp.hasTag('cable') || blockNeighborUp.hasTag('cable:up:down')));
        if(block.typeId !== 'nps_ec:farmer') permutation = permutation.withState('nps_ec:down', (blockNeighborDown.hasTag('cable') || blockNeighborDown.hasTag('cable:up:down')));
    
        block.setPermutation(permutation);
    } catch { }
}