import { world, EntityComponentTypes } from "@minecraft/server"
import BossManager from './BossManager';

class EventHandlers {
  constructor() {
    this.setupListeners();
  }

  setupListeners() {
    // Handle entity spawn event
    world.afterEvents.entitySpawn.subscribe((args) => {
      const { entity } = args;
      const entityId = entity.id;
      const typeId = entity.typeId;
      try {
        if (entity.getProperty('lv_mo:death')) return;
      } catch (error) {}
      if (BossManager.bossTypes.has(typeId)) {
        // Register the entity as a boss if it's in bossTypes
        BossManager.registerBoss(entityId, typeId);
      }

    });

    // Handle entity hurt event
    world.afterEvents.entityRemove.subscribe((args) => {
      const { removedEntityId } = args;

      if (BossManager.bossRegistry.has(removedEntityId)) {
        //console.warn('dead entity')
        BossManager.removeBoss(removedEntityId);
      }
    });

    // Handle entity load event
    world.afterEvents.entityLoad.subscribe((args) => {
      const { entity } = args;
      const entityId = entity.id;
      const typeId = entity.typeId;

      if (BossManager.bossTypes.has(typeId)) {
        BossManager.registerBoss(entityId, typeId);
      }
    });
  }
}

const eventHandlers = new EventHandlers();
export { eventHandlers };

