import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { EventEmitter } from "events";
import { logger } from "../../../core/monitoring/logger.js";
class SwarmRegistry extends EventEmitter {
  static instance = null;
  swarms = /* @__PURE__ */ new Map();
  constructor() {
    super();
    logger.info("SwarmRegistry initialized");
  }
  static getInstance() {
    if (!SwarmRegistry.instance) {
      SwarmRegistry.instance = new SwarmRegistry();
    }
    return SwarmRegistry.instance;
  }
  /**
   * Register a swarm coordinator
   */
  registerSwarm(coordinator, description) {
    const swarmId = this.generateSwarmId();
    const registration = {
      id: swarmId,
      coordinator,
      startTime: Date.now(),
      status: "active",
      description
    };
    this.swarms.set(swarmId, registration);
    this.emit("swarmRegistered", registration);
    logger.info(`Swarm registered: ${swarmId} - ${description}`);
    return swarmId;
  }
  /**
   * Unregister a swarm
   */
  unregisterSwarm(swarmId) {
    const swarm = this.swarms.get(swarmId);
    if (swarm) {
      this.swarms.delete(swarmId);
      this.emit("swarmUnregistered", { id: swarmId });
      logger.info(`Swarm unregistered: ${swarmId}`);
    }
  }
  /**
   * Get a specific swarm by ID
   */
  getSwarm(swarmId) {
    return this.swarms.get(swarmId) || null;
  }
  /**
   * List all active swarms
   */
  listActiveSwarms() {
    return Array.from(this.swarms.values()).filter(
      (swarm) => swarm.status === "active"
    );
  }
  /**
   * Update swarm status
   */
  updateSwarmStatus(swarmId, status) {
    const swarm = this.swarms.get(swarmId);
    if (swarm) {
      swarm.status = status;
      this.emit("swarmStatusChanged", { id: swarmId, status });
      logger.debug(`Swarm ${swarmId} status updated: ${status}`);
    }
  }
  /**
   * Get swarm statistics
   */
  getStatistics() {
    const active = this.listActiveSwarms();
    const completed = Array.from(this.swarms.values()).filter(
      (s) => s.status === "completed"
    );
    const totalUptime = active.reduce(
      (sum, swarm) => sum + (Date.now() - swarm.startTime),
      0
    );
    return {
      totalSwarms: this.swarms.size,
      activeSwarms: active.length,
      completedSwarms: completed.length,
      averageUptime: active.length > 0 ? totalUptime / active.length : 0
    };
  }
  /**
   * Cleanup all swarms and reset registry
   */
  cleanup() {
    this.swarms.clear();
    logger.info("SwarmRegistry cleaned up");
  }
  generateSwarmId() {
    return `swarm_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
  }
}
var swarm_registry_default = SwarmRegistry;
export {
  SwarmRegistry,
  swarm_registry_default as default
};
//# sourceMappingURL=swarm-registry.js.map
