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

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:table", {
        onPlace: e => {
            const { player, block, previousBlock } = e;

            if ( block.north().hasTag("table") ) {
                block.setPermutation(block.permutation.withState("r:north",true));
                block.north().setPermutation(block.north().permutation.withState("r:south",true));
            }
            if ( block.south().hasTag("table") ) {
                block.setPermutation(block.permutation.withState("r:south",true));
                block.south().setPermutation(block.south().permutation.withState("r:north",true));
            }
            if ( block.west().hasTag("table") ) {
                block.setPermutation(block.permutation.withState("r:west",true));
                block.west().setPermutation(block.west().permutation.withState("r:east",true));
            }
            if ( block.east().hasTag("table") ) {
                block.setPermutation(block.permutation.withState("r:east",true));
                block.east().setPermutation(block.east().permutation.withState("r:west",true));
            }
        },
        onPlayerBreak: e => {
            const { player, block, dimension } = e;
            if ( block.north().hasTag("table") ) {
                block.north().setPermutation(block.north().permutation.withState("r:south",false));
            }
            if ( block.south().hasTag("table") ) {
                block.south().setPermutation(block.south().permutation.withState("r:north",false));
            }
            if ( block.west().hasTag("table") ) {
                block.west().setPermutation(block.west().permutation.withState("r:east",false));
            }
            if ( block.east().hasTag("table") ) {
                block.east().setPermutation(block.east().permutation.withState("r:west",false));
            }
        },
    });
});