import { world } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// CropSpawnEntity class defines blocks that spawn entities based on certain conditions, such as random ticks.
export class CropSpawnEntity {
    // constructor with all properties
    constructor(config) {
        this.config = config;
    }
    // Method to register the crop spawn entity configuration
    static registerBehavior(config) {
        const eventEntity = new CropSpawnEntity(config);
        this.entities.push(eventEntity);
    }
    // Initialization method to register the custom block component and handle block states
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            // Register the custom spawn entity block component that triggers on random ticks
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:spawn_entity`, {
                onRandomTick: (event) => this.handleRandomTick(event),
            });
        });
    }
    // Method to handle random tick events and spawn entities if conditions are met
    static handleRandomTick(event) {
        const block = event.block;
        if (!block)
            return;
        // Get the spawn entity configuration for the block's type
        const spawnEntity = this.getSpawnEntityByIdentifier(block.typeId)?.config;
        if (!spawnEntity)
            return;
        if (block.permutation.getState(`${namespace}:spawn_entity`) == false)
            return;
        // Proceed to spawn the entity at the block's location
        this.spawnEntity(block, spawnEntity);
    }
    // Helper method to find a CropSpawnEntity by its identifier (block type)
    static getSpawnEntityByIdentifier(id) {
        return CropSpawnEntity.entities.find((find) => find.config.id === id); // Return the entity configuration if found
    }
    // Method to spawn the entity at the block's location
    static spawnEntity(block, spawnEntity) {
        let location = block.center(); // Get the center position of the block for entity spawning
        if (spawnEntity.spawnOffset) {
            location = {
                x: block.center().x + spawnEntity.spawnOffset.x,
                y: block.center().y + spawnEntity.spawnOffset.y,
                z: block.center().z + spawnEntity.spawnOffset.z,
            };
        }
        const entity = block.dimension.spawnEntity(spawnEntity.entity.id, location); // Spawn the entity at the location
        // Trigger any specified events on the spawned entity
        if (spawnEntity.entity.events) {
            spawnEntity.entity.events.forEach((event) => {
                entity.triggerEvent(event); // Trigger each event on the entity
            });
        }
        // If the block is configured to destroy itself after spawning the entity, replace it with air
        if (spawnEntity.destroySelf) {
            block.dimension.runCommandAsync(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        }
        // If the entity should not spawn repeatedly, mark the block as inactive for spawning
        if (spawnEntity.repeat === false) {
            block.setPermutation(block.permutation.withState(`${namespace}:spawn_entity`, false));
        }
    }
}
CropSpawnEntity.entities = [];
// Map to track the block states (whether they can spawn entities or not)
CropSpawnEntity.blockStates = new Map();
