// Import necessary modules from Minecraft server API
import { world, ItemStack, BlockPermutation } from '@minecraft/server';

// Subscribe to the 'worldInitialize' event to register custom components
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    // Register a custom component named lore_hrc:on_interact for slab interaction 
    blockComponentRegistry.registerCustomComponent('lore_hrc:on_interact', {
        // Define the behavior when a player interacts with the slab
        onPlayerInteract(e) {
            // Destructure event data for easier access
            const { block, player, face } = e;

            // Get the equipment component for the player
            const equipment = player.getComponent('equippable');

            // Get the selected item from the player's mainhand
            const selectedItem = equipment.getEquipment('Mainhand');
            // Check if the selected item is a slab and the block is not already double
            if (selectedItem?.typeId === block.typeId && !block.permutation.getState('lore_hrc:double')) {
                // Check if the interaction is valid based on vertical half and face
                const verticalHalf = block.permutation.getState('minecraft:vertical_half');
                const isBottomUp = verticalHalf === 'bottom' && face === 'Up';
                const isTopDown = verticalHalf === 'top' && face === 'Down';
                if (isBottomUp || isTopDown) {
                    // Reduces item count if not in creative mode
                    if (player.getGameMode() !== "creative") {
                        if (selectedItem.amount > 1) {
                            selectedItem.amount -= 1;
                            equipment.setEquipment('Mainhand', selectedItem);
                        } else if (selectedItem.amount === 1) {
                            equipment.setEquipment('Mainhand', undefined); // Clear the slot if only 1 item left
                        }
                    }
                    // Set block to double and remove water if present
                    block.setPermutation(block.permutation.withState('lore_hrc:double', true));
                    block.setWaterlogged(false);
                    // Play sound effect
                    player.playSound('use.stone');
                }
                if (face === 'North' && block.north(1).typeId === "minecraft:air") {
                    block.north(1).setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": verticalHalf}))
                }
                else if (face === 'East' && block.east(1).typeId === "minecraft:air") {
                    block.east(1).setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": verticalHalf}))
                }
                else if (face === 'West' && block.west(1).typeId === "minecraft:air") {
                    block.west(1).setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": verticalHalf}))
                }
                else if (face === 'South' && block.south(1).typeId === "minecraft:air") {
                    block.south(1).setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": verticalHalf}))
                }
            }

            // Check if the selected item is a water bucket and the block is not waterlogged or double
            if (selectedItem?.typeId === 'minecraft:water_bucket' && !block.permutation.getState('lore_hrc:waterlogged') && !block.permutation.getState('lore_hrc:double')) {
                // Play sound effect
                player.playSound('bucket.empty_water');
                // If not in creative mode, replace water bucket with empty bucket
                if (player.getGameMode() !== "creative") {
                    equipment.setEquipment('Mainhand', new ItemStack('minecraft:bucket', 1));
                }
                // Set block to waterlogged and place corresponding structure
                block.setPermutation(block.permutation.withState('lore_hrc:waterlogged', true));
                const verticalHalf = block.permutation.getState('minecraft:vertical_half');
                // Split the block identifier and use only the part after the colon
                const slabType = block.typeId.split(':')[1];
                // These structures contains your slab waterlogged, made with an NBT editor
                const structureName = (verticalHalf === 'bottom') ? `mystructure:${slabType}_bottomSlab` : `mystructure:${slabType}_topSlab`;
                const { x, y, z } = block;
                world.structureManager.place(structureName, e.dimension, { x, y, z });
            }
        }
    });
});