import { BlockPermutation, system, world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// TallBlock class handles the behavior of tall blocks that occupy two block spaces, such as large plants or structures.
export class TallBlock {
    constructor() {
        TallBlock.initialize();
    }
    // Initialization method to register the custom tall block component and related events
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:tall_block`, {
                beforeOnPlayerPlace: this.placeBlock,
                // Handle the tick event to generate the second block when appropriate
                onTick: this.generateBlock,
            });
        });
        // Subscribe to the block destruction event to handle breaking tall blocks
        world.beforeEvents.playerBreakBlock.subscribe(this.destroyBlock);
    }
    // Method to handle placing a tall block that spans two blocks (e.g., one block below, one above)
    static placeBlock(event) {
        const blockName = event.permutationToPlace.type.id; // Get the name of the block being placed
        const states = event.block.permutation.getAllStates(); // Get all the current states of the block
        // If there is no air block above the current block, cancel the placement
        if (event.block.above()?.typeId !== "minecraft:air") {
            event.cancel = true; // Prevent the block from being placed
            return;
        }
        // Set the block above with the same block type, but mark it as the "top" half of the tall block
        event.block
            .above()
            ?.setPermutation(BlockPermutation.resolve(`${blockName}`, states).withState(`${namespace}:half`, "top"));
    }
    // Method to automatically generate the second (top) part of the tall block during the tick event
    static generateBlock(event) {
        const blockName = event.block.typeId; // Get the type ID of the current block
        const states = event.block.permutation.getAllStates(); // Get the block's current states
        const blockAbove = event.block.above(); // Get the block directly above the current block
        // If the block above is not air or is of a different type, do nothing
        if (blockAbove?.typeId !== "minecraft:air" && blockAbove?.typeId !== blockName) {
            return;
        }
        // Place the same block above, marking it as the "top" part of the tall block
        blockAbove?.setPermutation(BlockPermutation.resolve(`${blockName}`, states).withState(`${namespace}:half`, "top"));
    }
    // Method to handle breaking a tall block, ensuring both the top and bottom parts are destroyed
    static destroyBlock(event) {
        // If the block being broken is not the "top" part, do nothing
        if (event.block.permutation.getState(`${namespace}:half`) !== "top")
            return;
        const blockName = event.block.typeId; // Get the block type of the top part
        const below = event.block.below(); // Get the block directly below the one being destroyed
        // If the block below is the same type, remove it as well
        if (below?.typeId === `${blockName}`) {
            system.runTimeout(() => {
                // Run a command to set the block below to air (destroy it)
                below?.dimension.runCommandAsync(`/setblock ${below.x} ${below.y} ${below.z} air destroy`);
            }, 1); // Slight delay to ensure proper destruction
        }
    }
}
