import { GameMode, } from '@minecraft/server';
function isUpper(permutation) {
    const upperBlockBit = permutation.getState('twb_wisu:upper_block_bit');
    if (upperBlockBit === undefined)
        return undefined;
    return (!!upperBlockBit);
}
export default class TallFlowerComponent {
    beforeOnPlayerPlace(e) {
        const { block } = e;
        const above = block.above();
        if (!above || !above.isAir) {
            e.cancel = true;
        }
    }
    onPlace(e) {
        const { block } = e;
        // Check whether this is the lower part of a plant.
        if (isUpper(block.permutation) !== false)
            return;
        // Check whether there is air above the plant.
        const above = block.above();
        if (!above || (!above.isAir && above.permutation.getState('upper_block_bit') !== true))
            return;
        const newPermutation = block.permutation.withState('twb_wisu:upper_block_bit', true);
        above.setPermutation(newPermutation);
    }
    onPlayerDestroy(e) {
        if (isUpper(e.destroyedBlockPermutation)) {
            // If the upper part is destroyed, we need to destroy the lower part as well.
            // The other way around is handled by a placement_filter in the block JSON.
            const below = e.block.below();
            if (!below || !e.destroyedBlockPermutation.matches(below.typeId))
                return;
            const isCreative = e.player && e.player.getGameMode() === GameMode.creative;
            if (isCreative) {
                // Prevent item from dropping
                below.setType('minecraft:air');
            }
            else {
                e.dimension.runCommand(`/setblock ${below.x} ${below.y} ${below.z} air destroy`);
            }
        }
    }
}
