import { world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// DripstoneBlock class to handle custom behavior for placing and destroying dripstone blocks
export class CandleStandBlock {
    constructor() {
        CandleStandBlock.initialize();
    }
    // Method to initialize custom components and event handling for dripstone blocks
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:candle_stand`, {
                // Handle events before a player places a block
                beforeOnPlayerPlace: (event) => this.handleBeforePlacement(event),
                onPlayerDestroy: (event) => this.handleDestroy(event),
            });
        });
    }
    // Handle logic before a player places a candle holder block
    static handleBeforePlacement(event) {
        const block = event.block; // Block the player is interacting with
        const upBlock = block.above(); // Block above the current one
        const downBlock = block.below(); // Block below the current one
        const tag = `${namespace}:candle_stand`;
        if (downBlock?.hasTag(tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:base`, false);
            downBlock.setPermutation(downBlock.permutation.withState(`${namespace}:top`, false));
        }
        if (upBlock?.hasTag(tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:top`, false);
            upBlock.setPermutation(upBlock.permutation.withState(`${namespace}:base`, false));
        }
    }
    // Handle logic when a player destroys a candle holder block
    static handleDestroy(event) {
        const block = event.block; // Block the player is interacting with
        const tag = `${namespace}:candle_stand`;
        const upBlock = block.above(); // Block above the current one
        const downBlock = block.below(); // Block below the current one
        if (downBlock?.hasTag(tag)) {
            downBlock.setPermutation(downBlock.permutation.withState(`${namespace}:top`, true));
        }
        if (upBlock?.hasTag(tag)) {
            upBlock.setPermutation(upBlock.permutation.withState(`${namespace}:base`, true));
        }
    }
}
