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

function handlePlacement(event) {
    const { block } = event;

    system.run(() => {
        if (!block) {
            return;
        }
        
        if (!(block.hasTag('pu_biomes:tall_bush') || 
              block.typeId === 'pu_biomes:horror_hand_branch' || 
              block.typeId === 'pu_biomes:horror_wing_branch')) {
            return;
        }

        if (block.typeId === 'pu_biomes:horror_wing_branch') {
            const blockFace = block.permutation.getState('minecraft:block_face');
            let sideBlock;
            
            switch (blockFace) {
                case 'north': sideBlock = block.north(); break;
                case 'south': sideBlock = block.south(); break;
                case 'east':  sideBlock = block.east();  break;
                case 'west':  sideBlock = block.west();  break;
                default:
                    return;
            }

            if (sideBlock.typeId !== 'minecraft:air') {
                block.setPermutation(BlockPermutation.resolve('minecraft:air'));
                return;
            }
            
            let blockStates = block.permutation.getAllStates();
            blockStates['pu_biomes:right_block_bit'] = true;
            
            let sidePermutation = BlockPermutation.resolve(block.typeId, blockStates);
            sideBlock.setPermutation(sidePermutation);
            return;
        }

        const aboveBlock = block.above();
        if (aboveBlock.typeId !== 'minecraft:air') {
            block.setPermutation(BlockPermutation.resolve('minecraft:air'));
            return;
        }
        
        let blockStates = block.permutation.getAllStates();
        blockStates['pu_biomes:upper_block_bit'] = true;
        
        let upperPermutation = BlockPermutation.resolve(block.typeId, blockStates);
        aboveBlock.setPermutation(upperPermutation);
    });
}

function handleBreak(event) {
    const { block } = event;

    system.run(() => {
        const aboveBlock = block.above();
        if (aboveBlock) {
            if (aboveBlock.hasTag('pu_biomes:tall_bush') || aboveBlock.typeId === 'pu_biomes:horror_hand_branch') {
                aboveBlock.setPermutation(BlockPermutation.resolve('minecraft:air'));
            }
        }

        const belowBlock = block.below();
        if (belowBlock) {
            if (belowBlock.hasTag('pu_biomes:tall_bush') || belowBlock.typeId === 'pu_biomes:horror_hand_branch') {
                belowBlock.setPermutation(BlockPermutation.resolve('minecraft:air'));
            }
        }
    });
}

function handleBeforeBreak(event) {
    const { block } = event;
    if (block.typeId === 'pu_biomes:horror_wing_branch') {
        const blockFace = block.permutation.getState('minecraft:block_face');
        const rightBlockBit = block.permutation.getState('pu_biomes:right_block_bit');
        
        const directions = {
            north: block.south(),
            south: block.north(),
            east: block.west(),
            west: block.east()
        };
        
        const targetBlock = rightBlockBit ? directions[blockFace] : block[blockFace]();
        if (targetBlock && targetBlock.typeId === 'pu_biomes:horror_wing_branch') {
            system.run(() => {
                targetBlock.setPermutation(BlockPermutation.resolve('minecraft:air'));
            });
        } else {
        }
    }
}

world.afterEvents.playerPlaceBlock.subscribe(handlePlacement);
world.afterEvents.playerBreakBlock.subscribe(handleBreak);
world.beforeEvents.playerBreakBlock.subscribe(handleBeforeBreak);