import { DimensionTypes, world } from '@minecraft/server';
import { TargetController } from './targetController';
/**
 * A static class to manage default targeting rules for entities.
 *
 * Use `TargetRegistry.register('twb_wisu:my_entity', scanOptions)` to register an entity type.
 *
 * Make sure `TargetRegistry.setup()` has been called before use.
 */
export default class TargetRegistry {
    // Prevent accidental initialization of this class; it has only static methods.
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    constructor() { }
    /**
     * Initializes all event hooks required for the TargetRegistry to work.
     */
    static setup() {
        // Make sure hooks are never registered twice
        if (this.hasBeenSetup)
            return;
        world.afterEvents.entityLoad.subscribe((e) => this.onEntityLoad(e.entity));
        world.afterEvents.entitySpawn.subscribe((e) => this.onEntityLoad(e.entity));
        world.beforeEvents.entityRemove.subscribe((e) => this.onEntityRemove(e.removedEntity));
        this.hasBeenSetup = true;
    }
    /**
     * Register an entity type to naturally target other entities.
     * @param entityId The typeId of the entity to register
     * @param options Scan options
     */
    static register(entityId, options) {
        this.ENTITIES.set(entityId, options);
        for (const dimensionType of DimensionTypes.getAll()) {
            const dimension = world.getDimension(dimensionType.typeId);
            dimension.getEntities({ type: entityId }).forEach((entity) => TargetController.get(entity).start(options));
        }
    }
    /**
     * Get the scan options registered for this entity type.
     * @param entityId The typeId of an entity
     * @returns Scan options, or undefined if this entity type is not registered
     */
    static get(entityId) {
        return TargetRegistry.ENTITIES.get(entityId);
    }
    static onEntityLoad(entity) {
        const options = TargetRegistry.get(entity.typeId);
        if (!options)
            return;
        const controller = TargetController.get(entity);
        controller.start(options);
    }
    static onEntityRemove(entity) {
        const controller = TargetController.get(entity);
        controller?.unload();
    }
}
TargetRegistry.ENTITIES = new Map();
TargetRegistry.hasBeenSetup = false;
