import { BlockPermutation, GameMode, system, world } from "@minecraft/server";
import { getCardinalDirection, removeItemFromInventory } from "../../helper/utils";
export class PlaceOnWaterBlock {
    // Constructor to initialize a PlaceOnWaterBlock with its properties
    constructor({ id, sound, cardinalDirection = false, }) {
        this.id = id;
        this.sound = sound;
        this.cardinalDirection = cardinalDirection;
    }
    static registerBehavior(config) {
        const block = new PlaceOnWaterBlock(config);
        this.register(block);
    }
    // Method to register a PlaceOnWaterBlock
    static register(block) {
        PlaceOnWaterBlock.blocks.push(block);
    }
    // Initialization method to register item use event with the world
    static initialize() {
        world.beforeEvents.itemUseOn.subscribe((event) => {
            this.placeBlock(event); // Call placeBlock method when an item is used
        });
    }
    // Method that handles the logic for placing a block on top of water
    static placeBlock(event) {
        // Get block data using the item stack's type ID (removing "_item" suffix)
        const blockData = PlaceOnWaterBlock.getBlockByIdentifier(event.itemStack.typeId.split("_item")[0]);
        // If no matching block data is found, exit early
        if (blockData === undefined)
            return;
        // Check if the block being interacted with is water or seagrass
        if (event.block.typeId === "minecraft:water" || event.block.typeId === "minecraft:seagrass") {
            const block = event.block.above(); // Get the block above the water
            // Exit if there is no block or if it's not air
            if (block === undefined || block.typeId !== "minecraft:air")
                return;
            // Run the block placement logic after a short delay to ensure proper timing
            system.runTimeout(() => {
                // Resolve the block permutation (its state data)
                let permutation = BlockPermutation.resolve(event.itemStack.typeId.split("_item")[0]);
                // Handle cardinal direction if required by the block
                if (blockData.cardinalDirection) {
                    // Get the player's view direction and convert it to a cardinal direction
                    const cardinalDirection = getCardinalDirection(event.source.getViewDirection());
                    // Set the block's cardinal direction state
                    permutation = permutation.withState("minecraft:cardinal_direction", cardinalDirection);
                }
                // Set the block's permutation (place the block on the water)
                block.setPermutation(permutation);
                // Play the block placement sound at the block's location
                block.dimension.playSound(blockData.sound, block.location);
            }, 1);
            // If the player is in creative mode, no need to remove the item
            if (event.source.getGameMode() === GameMode.creative)
                return;
            // Run a delayed task to remove the item from the player's inventory in survival mode
            system.runTimeout(() => {
                removeItemFromInventory(event.source, event.itemStack.typeId);
            }, 1);
        }
    }
    // Method to retrieve block data by its identifier
    static getBlockByIdentifier(identifier) {
        // Find the matching block by its ID in the registered blocks list
        return PlaceOnWaterBlock.blocks.find((block) => block.id === identifier);
    }
}
// Array to store all registered PlaceOnWaterBlock instances
PlaceOnWaterBlock.blocks = [];
