import { system } from "@minecraft/server";
import { namespace } from "../../../config/_config";
export class ScriptEventEntity {
    // Updated constructor
    constructor(config) {
        this.config = config;
    }
    // Updated registerBehavior method
    static registerBehavior(config) {
        const entity = new ScriptEventEntity(config);
        this.entities.push(entity);
    }
    // Initialize the system to listen for events and check if any match registered behaviors.
    static initialize() {
        system.afterEvents.scriptEventReceive.subscribe((event) => {
            // Ensure we are listening for the correct event ID (namespace + custom event name).
            if (event.id === `${namespace}:entity_event`) {
                // Find the entity by its typeId in the registered entities list
                const entity = this.entities.find((registeredEntity) => event.sourceEntity?.typeId === registeredEntity.config.id);
                if (entity) {
                    // Now find the specific script event by matching the message
                    const scriptEvent = entity.config.events.find((scriptEvent) => scriptEvent.message === event.message);
                    // If the message matches, trigger the action for the event
                    if (scriptEvent) {
                        scriptEvent.action(event.sourceEntity);
                    }
                }
            }
        });
    }
}
ScriptEventEntity.entities = [];
