// @bun
import {
  init_debug,
  logForDebugging
} from "./chunk-byd20vdq.js";
import"./chunk-kb3758f7.js";
import"./chunk-3j8w0rq8.js";
import"./chunk-tj0dzck2.js";
import"./chunk-ns1htxgd.js";
import"./chunk-ztqzmfx1.js";
import"./chunk-6k1rsk85.js";
import"./chunk-nxzx0ey9.js";
import"./chunk-yes1my80.js";
import {
  __esm
} from "./chunk-hhsxm2yr.js";

// src/services/collab/collab.ts
function getPeers() {
  return Array.from(_peers.values());
}
function getActivePeers() {
  const now = Date.now();
  return getPeers().filter((p) => now - p.lastSeen < PEER_TIMEOUT);
}
async function startCollab(opts = {}) {
  if (_running)
    return;
  _running = true;
  const port = opts.port || DEFAULT_PORT;
  logForDebugging(`collab: starting on port ${port}`, { level: "debug" });
  _broadcastTimer = setInterval(() => {
    announcePresence(port);
  }, opts.broadcastInterval || BROADCAST_INTERVAL);
  logForDebugging("collab: started", { level: "debug" });
}
function stopCollab() {
  _running = false;
  if (_broadcastTimer) {
    clearInterval(_broadcastTimer);
    _broadcastTimer = null;
  }
  _peers.clear();
  logForDebugging("collab: stopped", { level: "debug" });
}
function announcePresence(port) {
  logForDebugging(`collab: announcing on port ${port}`, { level: "debug" });
}
function getCollabStatus() {
  const peers = getActivePeers();
  if (!_running)
    return "Collaboration mode: not running";
  if (peers.length === 0)
    return "Collaboration mode: running, no peers found";
  return `Collaboration mode: ${peers.length} peer(s) connected
${peers.map((p) => `  \u2022 ${p.name} (${p.host}) \u2014 ${p.project}`).join(`
`)}`;
}
var DEFAULT_PORT = 48910, BROADCAST_INTERVAL = 5000, PEER_TIMEOUT = 15000, _peers, _broadcastTimer = null, _running = false;
var init_collab = __esm(() => {
  init_debug();
  _peers = new Map;
});

// src/commands/collab/collab.ts
var call = async (args, context) => {
  const cmd = args?.trim() || "status";
  if (cmd === "start") {
    await startCollab();
    return {
      type: "text",
      value: "\u2713 Collaboration started. Broadcasting on LAN..."
    };
  }
  if (cmd === "stop") {
    stopCollab();
    return { type: "text", value: "\u2713 Collaboration stopped." };
  }
  if (cmd === "peers") {
    const peers = getActivePeers();
    return {
      type: "text",
      value: peers.length ? peers.map((p) => `\u2022 ${p.name} (${p.host})`).join(`
`) : "No peers found."
    };
  }
  return { type: "text", value: getCollabStatus() };
};
var init_collab2 = __esm(() => {
  init_collab();
});
init_collab2();

export {
  call
};

//# debugId=C4B4B6EA782944D464756E2164756E21
//# sourceMappingURL=chunk-4w6be4hf.js.map
