import { system, world } from "@minecraft/server";
import { emitDynamicLight } from "../../helper/emitDynamicLight";
import { namespace } from "../../../config/_config";
import { Dimensions } from "../../helper/utils";
/**
 * SummonEntity class defines entities that spawn a specific mob in the game
 * when within range of a player.
 *
 * @param {string} id - The unique identifier for the summon entity.
 * @param {string} mob - The ID of the mob that should be summoned and optional events to trigger on this entity
 * @param {number} range - The range within which players need to be for the mob to be summoned.
 * @param {string[]} particles - An optional array of particle effects to display when the entity is summoned.
 * @param {string} sound - An optional sound to play when the entity is summoned.
 */
export class SummonEntityEntity {
    constructor(config) {
        this.config = config;
    }
    /**
     * Registers a summon entity with the specified configuration.
     * @param {SummonEntityEntityOptions} config - The configuration for the summon entity.
     */
    static registerBehavior(config) {
        const summonEntity = new SummonEntityEntity(config);
        this.entities.push(summonEntity);
    }
    /**
     * Initializes the summon behavior for registered entities by scanning through
     * the overworld, nether, and end dimensions at regular intervals.
     */
    static initialize() {
        // Repeatedly run the logic every 20 ticks (approx 1 second).
        system.runInterval(() => {
            for (const dimension in Dimensions) {
                for (const config of this.entities) {
                    Dimensions[dimension].getEntities({ type: config.config.id }).forEach((entity) => {
                        // Find players near the entity within the specified range.
                        const nearbyPlayers = entity.dimension.getPlayers({
                            location: entity.location,
                            maxDistance: config.config.range,
                        });
                        if (nearbyPlayers.length === 0)
                            return; // If no players are nearby, skip.
                        world.sendMessage(`${config.config.mob.id}`);
                        // Summon the specified mob at the entity's location.
                        const spawnEntity = entity.dimension.spawnEntity(config.config.mob.id, entity.location);
                        // Emit dynamic light around the newly spawned entity.
                        emitDynamicLight(15, spawnEntity, 4);
                        // Trigger despawn event on the original entity after the mob is summoned.
                        entity.triggerEvent(`${namespace}:despawn`);
                        // Trigger any additional events on the newly spawned mob.
                        if (config.config.mob.events) {
                            config.config.mob.events.forEach((event) => {
                                spawnEntity.triggerEvent(event);
                            });
                        }
                        if (config.config.spawnEffects) {
                            // Spawn particle effects if defined for the summon.
                            if (config.config.spawnEffects.particles) {
                                config.config.spawnEffects.particles.forEach((particle) => {
                                    spawnEntity.dimension.spawnParticle(particle, entity.location);
                                });
                            }
                            // Play sound effect if defined for the summon.
                            if (config.config.spawnEffects.sounds) {
                                config.config.spawnEffects.sounds.forEach((sound) => {
                                    spawnEntity.dimension.playSound(sound, entity.location);
                                });
                            }
                        }
                    });
                }
            }
        }, 20); // Run every 20 ticks (1 second).
    }
}
SummonEntityEntity.entities = [];
