import { world } from "@minecraft/server";
import { namespace } from "../../../config/_config";
import { summonEntitiesInRange, } from "../../helper/summonFromLocation";
export class SpawnerBlock {
    // Constructor to initialize a SpawnerBlock with its properties
    constructor({ id, entities, onSpawn, }) {
        this.id = id;
        this.entities = entities;
        this.onSpawn = onSpawn;
    }
    static registerBehavior(config) {
        const block = new SpawnerBlock(config);
        this.register(block);
    }
    // Method to register a SpawnerBlock
    static register(block) {
        SpawnerBlock.blocks.push(block);
    }
    // Initialization method to handle the custom spawner block logic when the world starts
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:spawner`, {
                // Handle the onTick event for the spawner block (this event runs each tick the block exists)
                onTick: (event) => {
                    const block = event.block; // The block that triggered the onTick event
                    const spawnBlock = this.getBlockByIdentifier(block.typeId); // Find the registered spawner block by its type ID
                    // Exit if the block is not a registered spawner block
                    if (!spawnBlock)
                        return;
                    // Summon entities in range using the registered spawner block's entity options
                    summonEntitiesInRange(block.location, block.dimension, spawnBlock.entities);
                    // Call the onSpawn function
                    if (spawnBlock.onSpawn) {
                        spawnBlock.onSpawn(block);
                    }
                    // Replace the block with air after the entities are spawned, removing the spawner block
                    block.setType("air");
                },
            });
        });
    }
    // Method to retrieve a registered SpawnerBlock by its identifier
    static getBlockByIdentifier(identifier) {
        return this.blocks.find((block) => block.id === identifier); // Return the spawner block if found
    }
}
// Array to store all registered SpawnerBlock instances
SpawnerBlock.blocks = [];
