import { world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// VineBlock class handles the behavior of vine-like blocks that can connect when placed or change state when destroyed.
export class VineBlock {
    constructor() {
        VineBlock.initialize();
    }
    // Initialization method to register the custom vine block component and related events
    static initialize() {
        // Subscribe to the world initialization event to register the custom block component
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:vine`, {
                // Handle event when a vine block is placed
                onPlace: (event) => {
                    this.onPlace(event);
                },
                // Handle event when a vine block is destroyed by a player
                onPlayerDestroy: (event) => {
                    this.onDestroy(event);
                },
            });
        });
    }
    // Method to handle the behavior when a vine block is placed
    static onPlace(event) {
        const above = event.block.above(); // Get the block directly above the one being placed
        const block = event.block; // The block that was placed
        // If the block above is the same type (another vine), update its state to show it's connected
        if (above?.typeId === block.typeId) {
            // Set the state of the block above to indicate that it's an "inner" vine (connected to the one below)
            above.setPermutation(above.permutation.withState(`${namespace}:connect`, "inner"));
        }
    }
    // Method to handle the behavior when a vine block is destroyed by a player
    static onDestroy(event) {
        const above = event.block.above(); // Get the block directly above the one being destroyed
        const block = event.destroyedBlockPermutation; // The permutation (state) of the block that was destroyed
        // If the block above is the same type (another vine), update its state to show it's now an "end" vine
        if (above?.typeId === block.type.id) {
            // Set the state of the block above to indicate it's an "end" vine (no longer connected to a block below)
            above.setPermutation(above.permutation.withState(`${namespace}:connect`, "end"));
        }
    }
}
