import { world } from "@minecraft/server";
import { namespace } from "../../../config/_config";
export class ConditionCheckBlock {
    constructor(config) {
        this.id = config.id;
        this.events = config.events;
    }
    static registerBehavior(config) {
        const block = new ConditionCheckBlock(config);
        this.register(block);
    }
    static register(block) {
        ConditionCheckBlock.blocks.push(block);
    }
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:condition_check`, {
                onTick: (event) => {
                    this.conditionCheckBlock(event);
                },

            });
        });
    }
    static conditionCheckBlock(event) {
        const block = event.block;
        const conditionCheckBlock = ConditionCheckBlock.blocks.find((block) => block.id === event.block.typeId);
        // new check for state
        if (conditionCheckBlock) {
            conditionCheckBlock.events.forEach((event) => {
                if (event.state && block.permutation.getState(event.state.state) !== event.state.value) {
                    return;
                }
                else {
                    if (event.condition(block)) {
                        event.event(block);
                    }
                }
            });
        }
    }
}
ConditionCheckBlock.blocks = [];
