import { Block, Dimension } from "@minecraft/server";
import { blockNeighbors, setBlockToAir } from "../../library/blockManager"
/**
 * Evento del componente destroyBlockPositionComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const destroyBlockPositionEvent = ({block, dimension}) => {
    try {
        const {
            blockNeighborDown, blockNeighborUp,
            blockNeighborEast, blockNeighborWest,
            blockNeighborNorth, blockNeighborSouth
        } = blockNeighbors(block)
    
        if(!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;
    
        const placeState = block.permutation.getState('minecraft:block_face');
    
        if (placeState === 'up' && !blockNeighborDown.hasTag('connector')) {
            setBlockToAir(block, dimension);
        } else if (placeState === 'down' && !blockNeighborUp.hasTag('connector')) {
            setBlockToAir(block, dimension);
        } else if (placeState === 'south' && !blockNeighborNorth.hasTag('connector')) {
            setBlockToAir(block, dimension);
        } else if (placeState === 'north' && !blockNeighborSouth.hasTag('connector')) {
            setBlockToAir(block, dimension);
        } else if (placeState === 'west' && !blockNeighborEast.hasTag('connector')) {
            setBlockToAir(block, dimension);
        } else if (placeState === 'east' && !blockNeighborWest.hasTag('connector')) {
            setBlockToAir(block, dimension);
        }
    } catch { }
}