import { system, world, } from "@minecraft/server";
import { Dimensions } from "../../helper/utils";
export class EventEntity {
    constructor(config) {
        this.config = config;
    }
    static registerBehavior(config) {
        const eventEntity = new EventEntity(config);
        this.entities.push(eventEntity);
    }
    static getEntity(id) {
        return EventEntity.entities.find((entity) => entity.config.id === id);
    }
    static initialize() {
        const dimensions = ["overworld", "nether", "the_end"];
        system.runInterval(() => {
            for (const dimension in Dimensions) {
                for (const config of this.entities) {
                    Dimensions[dimension].getEntities({ type: config.config.id }).forEach((entity) => {
                        if (config && config.config.onEntityTick) {
                            config.config.onEntityTick(entity);
                        }
                    });
                }
            }
        }, 1);
        world.afterEvents.entityHitEntity.subscribe((event) => {
            const entity = EventEntity.getEntity(event.damagingEntity.typeId);
            if (entity && entity.config.onEntityHit) {
                entity.config.onEntityHit(event.damagingEntity, event.hitEntity);
            }
        });
        world.afterEvents.entityHurt.subscribe((event) => {
            const entity = EventEntity.getEntity(event.hurtEntity.typeId);
            if (entity && entity.config.onEntityHurt) {
                entity.config.onEntityHurt(event.hurtEntity, event.damageSource);
            }
        });
        world.afterEvents.entityDie.subscribe((event) => {
            const entity = EventEntity.getEntity(event.deadEntity.typeId);
            if (entity && entity.config.onEntityDie) {
                entity.config.onEntityDie(event.deadEntity, event.damageSource);
            }
        });
        world.afterEvents.entitySpawn.subscribe((event) => {
            const entity = EventEntity.getEntity(event.entity.typeId);
            if (entity && entity.config.onEntitySpawn) {
                entity.config.onEntitySpawn(event.entity);
            }
        });
    }
}
EventEntity.entities = [];
