import { world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// Still a mess
const breathableBlocks = ["minecraft:air"];
export class PipeConnectBlock {
    static initialize() {
        // Register custom block component when the world is initialized
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:pipe_connect`, {
                // Handle events before a player places a block
                beforeOnPlayerPlace: (event) => this.handleBeforePlacement(event),
            });
        });
        world.afterEvents.playerPlaceBlock.subscribe((event) => {
            this.handlePlaceAndDestroy(event);
        });
        world.afterEvents.playerBreakBlock.subscribe((event) => {
            this.handlePlaceAndDestroy(event);
        });
    }
    static handleBeforePlacement(event) {
        const neighbors = {
            above: breathableBlocks.includes(event.block.above().typeId),
            below: breathableBlocks.includes(event.block.below().typeId),
            north: breathableBlocks.includes(event.block.north().typeId),
            south: breathableBlocks.includes(event.block.south().typeId),
            east: breathableBlocks.includes(event.block.east().typeId),
            west: breathableBlocks.includes(event.block.west().typeId),
        };
        // Loop through each neighbor and set the state dynamically
        for (const [direction, isBreathable] of Object.entries(neighbors)) {
            const stateKey = `${namespace}:connect_${direction}`;
            event.permutationToPlace = event.permutationToPlace.withState(stateKey, !isBreathable);
        }
    }
    static handlePlaceAndDestroy(event) {
        // List of neighbor blocks to check
        const neighbors = [
            event.block.above(),
            event.block.below(),
            event.block.north(),
            event.block.south(),
            event.block.east(),
            event.block.west(),
        ];
        // Update each neighbor
        neighbors.forEach((neighbor) => {
            if (neighbor) {
                this.handleBlockUpdate(neighbor);
            }
        });
    }
    static handleBlockUpdate(block) {
        if (!block.hasTag(`${namespace}:pipe_connect`))
            return;
        const neighbors = {
            above: breathableBlocks.includes(block.above().typeId),
            below: breathableBlocks.includes(block.below().typeId),
            north: breathableBlocks.includes(block.north().typeId),
            south: breathableBlocks.includes(block.south().typeId),
            east: breathableBlocks.includes(block.east().typeId),
            west: breathableBlocks.includes(block.west().typeId),
        };
        // Loop through each neighbor and set the state dynamically
        for (const [direction, isBreathable] of Object.entries(neighbors)) {
            const stateKey = `${namespace}:connect_${direction}`;
            block.setPermutation(block.permutation.withState(stateKey, !isBreathable));
        }
    }
}
