import { world } from '@minecraft/server';
import { namespace } from '../../../config/_config';
export class CropSapling {
    constructor(config) {
        this.config = config;
    }
    static registerBehavior(config) {
        const block = new CropSapling(config);
        CropSapling.blocks.push(block);
    }
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:sapling`, {
                onTick: (event) => {
                    const sapling = CropSapling.getSaplingByIdentifier(event.block.typeId);
                    if (sapling) {
                        CropSapling.spawnTree(event, sapling);
                    }
                },
            });
        });
    }
    static spawnTree(event, sapling) {
        const location = event.block.location;
        const structure = sapling.config.structures[Math.floor(Math.random() * sapling.config.structures.length)];
        event.block.dimension.runCommandAsync(`structure load "${structure}" ${location.x - sapling.config.centerOffset} ${location.y} ${location.z - sapling.config.centerOffset}`);
    }
    static getSaplingByIdentifier(id) {
        return CropSapling.blocks.find((sapling) => sapling.config.id === id);
    }
}
CropSapling.blocks = [];
