import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { SwarmRegistry } from "../../integrations/ralph/monitoring/swarm-registry.js";
import { execSync } from "child_process";
class SimpleSwarmMonitor {
  refreshInterval = null;
  /**
   * Start simple text-based monitoring
   */
  start() {
    console.log("\u{1F9BE} Ralph Swarm Monitor (Text Mode)");
    console.log("=====================================");
    console.log("");
    console.log("Press Ctrl+C to quit");
    console.log("");
    this.displayStatus();
    this.refreshInterval = setInterval(() => {
      this.displayStatus();
    }, 5e3);
    process.on("SIGINT", () => {
      this.stop();
      process.exit(0);
    });
  }
  /**
   * Stop monitoring
   */
  stop() {
    if (this.refreshInterval) {
      clearInterval(this.refreshInterval);
    }
    console.log("\n\u{1F44B} Swarm monitoring stopped");
  }
  /**
   * Display current status
   */
  displayStatus() {
    const timestamp = (/* @__PURE__ */ new Date()).toLocaleTimeString();
    console.log(`
\u23F0 ${timestamp} - Swarm Status Update`);
    console.log("\u2500".repeat(50));
    try {
      const registry = SwarmRegistry.getInstance();
      const activeSwarms = registry.listActiveSwarms();
      const stats = registry.getStatistics();
      console.log(
        `\u{1F4CA} Registry Stats: ${stats.activeSwarms} active, ${stats.totalSwarms} total`
      );
      if (activeSwarms.length > 0) {
        console.log("\n\u{1F9BE} Active Swarms:");
        for (const swarm of activeSwarms) {
          const uptime = this.formatDuration(Date.now() - swarm.startTime);
          console.log(
            `   \u2022 ${swarm.id.substring(0, 8)}: ${swarm.status} (${uptime})`
          );
        }
      } else {
        console.log("\u274C No active swarms in registry");
      }
      try {
        const ralphProcesses = execSync(
          'ps aux | grep "ralph" | grep -v grep',
          { encoding: "utf8" }
        );
        if (ralphProcesses.trim()) {
          console.log("\n\u{1F50D} External Ralph Processes:");
          const processLines = ralphProcesses.split("\n").filter((line) => line.trim());
          processLines.slice(0, 3).forEach((line) => {
            const parts = line.split(/\s+/);
            console.log(
              `   PID ${parts[1]}: ${parts.slice(10).join(" ").slice(0, 50)}...`
            );
          });
          if (processLines.length > 3) {
            console.log(`   ... and ${processLines.length - 3} more processes`);
          }
        }
      } catch {
      }
      try {
        const recentCommits = execSync(
          'git log --oneline --since="1 hour ago" --pretty=format:"%h %an %s" | head -3',
          { encoding: "utf8", cwd: process.cwd() }
        );
        if (recentCommits.trim()) {
          console.log("\n\u{1F4DD} Recent Commits:");
          recentCommits.split("\n").filter((line) => line.trim()).forEach((line) => {
            console.log(`   ${line}`);
          });
        }
      } catch {
      }
    } catch (error) {
      console.log(`\u274C Status error: ${error.message}`);
    }
  }
  /**
   * Format duration string
   */
  formatDuration(ms) {
    const seconds = Math.floor(ms / 1e3);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    if (hours > 0) return `${hours}h ${minutes % 60}m`;
    if (minutes > 0) return `${minutes}m ${seconds % 60}s`;
    return `${seconds}s`;
  }
}
var simple_monitor_default = SimpleSwarmMonitor;
export {
  SimpleSwarmMonitor,
  simple_monitor_default as default
};
//# sourceMappingURL=simple-monitor.js.map
