import { writeJsonConfig } from "./config-writer-json.js";
import { writeTomlConfig } from "./config-writer-toml.js";
import { installViaClaudeCli } from "./config-writer-cli.js";
const SERVER_COMMAND = "npx";
const SERVER_ARGS = ["-y", "@staticn0va/wigolo"];
const JSON_SPECS = {
  cursor: { keyPath: ["mcpServers", "wigolo"] },
  vscode: { keyPath: ["servers", "wigolo"], extraEntryFields: { type: "stdio" } },
  zed: { keyPath: ["context_servers", "wigolo"] },
  "gemini-cli": { keyPath: ["mcpServers", "wigolo"] },
  windsurf: { keyPath: ["mcpServers", "wigolo"] },
  opencode: { keyPath: ["mcp", "wigolo"], extraEntryFields: { type: "local" } }
};
const CODEX_TABLE_PATH = ["mcp_servers", "wigolo"];
function buildEntry(extra) {
  return { command: SERVER_COMMAND, args: [...SERVER_ARGS], ...extra ?? {} };
}
async function applyOne(agent, opts) {
  const base = {
    id: agent.id,
    displayName: agent.displayName,
    configPath: agent.configPath
  };
  if (agent.installType === "cli-command") {
    const r2 = await installViaClaudeCli({ dryRun: opts.dryRun });
    return mapCliResult(base, r2);
  }
  if (agent.installType === "config-toml") {
    if (!agent.configPath) {
      return { ...base, ok: false, code: "NO_CONFIG_PATH", message: "no configPath for TOML agent" };
    }
    const r2 = await writeTomlConfig({
      path: agent.configPath,
      tablePath: CODEX_TABLE_PATH,
      entry: buildEntry(),
      dryRun: opts.dryRun
    });
    return mapTomlResult(base, r2);
  }
  if (!agent.configPath) {
    return { ...base, ok: false, code: "NO_CONFIG_PATH", message: "no configPath for JSON agent" };
  }
  const spec = JSON_SPECS[agent.id];
  if (!spec) {
    return { ...base, ok: false, code: "UNKNOWN_AGENT", message: `no JSON spec for ${agent.id}` };
  }
  const r = await writeJsonConfig({
    path: agent.configPath,
    keyPath: spec.keyPath,
    entry: buildEntry(spec.extraEntryFields),
    dryRun: opts.dryRun
  });
  return mapJsonResult(base, r);
}
function mapJsonResult(base, r) {
  return {
    ...base,
    ok: r.ok,
    code: r.code,
    message: r.message,
    dryRun: r.dryRun,
    backupPath: r.backupPath
  };
}
function mapTomlResult(base, r) {
  return {
    ...base,
    ok: r.ok,
    code: r.code,
    message: r.message,
    dryRun: r.dryRun,
    backupPath: r.backupPath
  };
}
function mapCliResult(base, r) {
  return {
    ...base,
    ok: r.ok,
    code: r.code ?? (r.ok ? "OK" : "CLAUDE_FAILED"),
    message: r.message,
    alreadyInstalled: r.alreadyInstalled,
    dryRun: r.dryRun,
    // Surface the fallback path so the install summary can show it instead of
    // the (null) cli-command configPath when host `claude` was absent.
    configPath: r.fallbackPath ?? base.configPath
  };
}
async function applyConfigs(detected, selected, opts = {}) {
  const byId = new Map(detected.map((a) => [a.id, a]));
  const results = [];
  for (const id of selected) {
    const agent = byId.get(id);
    if (!agent) continue;
    const r = await applyOne(agent, opts);
    results.push(r);
  }
  return results;
}
export {
  CODEX_TABLE_PATH,
  JSON_SPECS,
  applyConfigs
};
//# sourceMappingURL=config-writer.js.map