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

export function customGlassPane(block){
    if(!block.hasTag("bps_cg:glass_pane")) return;
    const state = getStateString(block);
    block.setPermutation(block.permutation.withState("bps_cg:attach_direction", state));
}


function getStateString(block){
    const dimension = block.dimension;
        const location = block.location;
        let stateString = "";
        
        const northBlock = dimension.getBlock({x:location.x, y:location.y, z:location.z - 1});
        const eastBlock = dimension.getBlock({x:location.x + 1, y:location.y, z:location.z});
        const southBlock = dimension.getBlock({x:location.x, y:location.y, z:location.z + 1});
        const westBlock = dimension.getBlock({x:location.x - 1, y:location.y, z:location.z});
        
        if(northBlock != null && northBlock != undefined && !northBlock.isAir && !northBlock.isLiquid) stateString = stateString + "N";
        if(eastBlock != null && eastBlock != undefined && !eastBlock.isAir && !eastBlock.isLiquid) stateString = stateString + "E";
        if(southBlock != null && southBlock != undefined && !southBlock.isAir && !southBlock.isLiquid) stateString = stateString + "S";
        if(westBlock != null && westBlock != undefined && !westBlock.isAir && !westBlock.isLiquid) stateString = stateString + "W";

        if(stateString.length === 0) stateString = stateString + "single";

        return stateString;
}