"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var discover_exports = {};
__export(discover_exports, {
  cachePathFor: () => cachePathFor,
  discoverAiAgents: () => discoverAiAgents,
  discoverFresh: () => discoverFresh,
  getDiscoveredAiAgents: () => getDiscoveredAiAgents,
  readDiskCache: () => readDiskCache,
  resetAiAgentDiscoveryCache: () => resetAiAgentDiscoveryCache,
  writeDiskCache: () => writeDiskCache
});
module.exports = __toCommonJS(discover_exports);
var import_node_fs = require("node:fs");
var import_promises = require("node:fs/promises");
var import_node_path = __toESM(require("node:path"), 1);
var import_which = require("../bin/which");
var import_message = require("../errors/message");
var import_logger = require("../logger/logger");
var import_json = require("../primordials/json");
const logger = (0, import_logger.getDefaultLogger)();
const KNOWN_AGENTS = [
  "claude",
  "codex",
  "gemini",
  "opencode"
];
const CACHE_TTL_MS = 60 * 60 * 1e3;
let inProcessCache;
function cachePathFor(repoRoot) {
  return import_node_path.default.join(repoRoot, ".cache", "agent-discovery.json");
}
async function discoverAiAgents(options = {}) {
  const { refresh = false, repoRoot = process.cwd() } = options;
  if (!refresh && inProcessCache) {
    return inProcessCache;
  }
  const cachePath = cachePathFor(repoRoot);
  if (!refresh) {
    const fromDisk = readDiskCache(cachePath);
    if (fromDisk) {
      inProcessCache = fromDisk;
      return fromDisk;
    }
  }
  const fresh = discoverFresh();
  inProcessCache = fresh;
  await writeDiskCache(cachePath, fresh);
  return fresh;
}
function discoverFresh() {
  const out = {};
  for (const name of KNOWN_AGENTS) {
    const found = (0, import_which.whichSync)(name);
    if (typeof found === "string" && found) {
      out[name] = found;
    }
  }
  return out;
}
function getDiscoveredAiAgents() {
  return inProcessCache;
}
function readDiskCache(cachePath) {
  if (!(0, import_node_fs.existsSync)(cachePath)) {
    return void 0;
  }
  try {
    const raw = (0, import_node_fs.readFileSync)(cachePath, "utf8");
    const parsed = (0, import_json.JSONParse)(raw);
    if (typeof parsed !== "object" || parsed === null || typeof parsed.writtenAt !== "number" || Date.now() - parsed.writtenAt > CACHE_TTL_MS) {
      return void 0;
    }
    return parsed.agents;
  } catch {
    return void 0;
  }
}
function resetAiAgentDiscoveryCache() {
  inProcessCache = void 0;
}
async function writeDiskCache(cachePath, agents) {
  try {
    await (0, import_promises.mkdir)(import_node_path.default.dirname(cachePath), { recursive: true });
    const payload = { agents, writtenAt: Date.now() };
    (0, import_node_fs.writeFileSync)(cachePath, (0, import_json.JSONStringify)(payload, void 0, 2) + "\n");
  } catch (e) {
    logger.error(`discoverAiAgents: cache write failed (${(0, import_message.errorMessage)(e)})`);
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  cachePathFor,
  discoverAiAgents,
  discoverFresh,
  getDiscoveredAiAgents,
  readDiskCache,
  resetAiAgentDiscoveryCache,
  writeDiskCache
});
