import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { CLAUDE_AGENTS_DIR, CCJK_CONFIG_DIR } from '../chunks/constants.mjs';
import { j as join } from './ccjk.bQ7Dh1g4.mjs';

const getProjectAgentsDir = (projectDir) => join(projectDir || process.cwd(), ".claude", "agents");
const GLOBAL_AGENTS_DIR = CLAUDE_AGENTS_DIR;
join(CCJK_CONFIG_DIR, "agents");
const AGENT_COLORS = ["blue", "green", "orange", "purple", "red", "yellow", "cyan", "pink"];
function agentToMarkdown(agent, options) {
  const name = generateAgentId(agent);
  const description = typeof agent.description === "string" ? agent.description : agent.description?.en || agent.description?.["zh-CN"] || "";
  const tools = options?.tools || [];
  if (agent.skills?.length) {
    tools.push(...agent.skills.map((s) => s.skillId || s.pluginId));
  }
  if (agent.mcpServers?.length) {
    tools.push(...agent.mcpServers.map((m) => `mcp__${m.serverName}`));
  }
  const color = options?.color || selectAgentColor(agent);
  const frontmatter = [
    "---",
    `name: ${name}`,
    `description: ${description}`
  ];
  if (tools.length > 0) {
    frontmatter.push(`tools: ${tools.join(", ")}`);
  }
  frontmatter.push(`color: ${color}`);
  frontmatter.push("---");
  const body = [];
  if (agent.persona) {
    body.push("");
    body.push(agent.persona);
  }
  if (agent.instructions) {
    body.push("");
    body.push("## Instructions");
    body.push("");
    body.push(agent.instructions);
  }
  if (agent.capabilities?.length) {
    body.push("");
    body.push("## Capabilities");
    body.push("");
    for (const cap of agent.capabilities) {
      body.push(`- ${formatCapability(cap)}`);
    }
  }
  if (agent.triggers?.length) {
    body.push("");
    body.push("## Triggers");
    body.push("");
    for (const trigger of agent.triggers) {
      body.push(`- ${trigger}`);
    }
  }
  return `${frontmatter.join("\n") + body.join("\n")}
`;
}
function selectAgentColor(agent) {
  const caps = agent.capabilities || [];
  if (caps.includes("code-generation") || caps.includes("code-review"))
    return "blue";
  if (caps.includes("testing"))
    return "green";
  if (caps.includes("debugging"))
    return "orange";
  if (caps.includes("deployment"))
    return "purple";
  if (caps.includes("documentation"))
    return "cyan";
  if (caps.includes("git-operations"))
    return "yellow";
  if (caps.includes("web-search") || caps.includes("api-integration"))
    return "pink";
  const hash = agent.id?.split("").reduce((a, c) => a + c.charCodeAt(0), 0) || 0;
  return AGENT_COLORS[hash % AGENT_COLORS.length];
}
function formatCapability(cap) {
  const labels = {
    "code-generation": "Code Generation",
    "code-review": "Code Review",
    "testing": "Testing",
    "documentation": "Documentation",
    "deployment": "Deployment",
    "debugging": "Debugging",
    "refactoring": "Refactoring",
    "git-operations": "Git Operations",
    "file-management": "File Management",
    "web-search": "Web Search",
    "api-integration": "API Integration"
  };
  return labels[cap] || cap;
}
async function writeAgentFile(agent, options) {
  const format = options?.format || "markdown";
  const agentId = generateAgentId(agent);
  const isGlobal = options?.global || false;
  const agentsDir = isGlobal ? GLOBAL_AGENTS_DIR : getProjectAgentsDir(options?.projectDir);
  const ext = format === "markdown" ? "md" : "json";
  const fileName = `${agentId}.${ext}`;
  const filePath = join(agentsDir, fileName);
  if (!existsSync(agentsDir)) {
    mkdirSync(agentsDir, { recursive: true });
  }
  let content;
  if (format === "markdown") {
    content = agentToMarkdown(agent, options);
  } else {
    content = JSON.stringify(agent, null, options?.pretty ? 2 : 0);
  }
  writeFileSync(filePath, content, "utf-8");
  return filePath;
}
function generateAgentId(agent) {
  if (agent.id) {
    return agent.id.toLowerCase().replace(/\s+/g, "-");
  }
  const name = typeof agent.name === "string" ? agent.name : agent.name?.en || agent.name?.["zh-CN"] || "unknown-agent";
  return name.toLowerCase().replace(/\s+/g, "-");
}
function getAgentsDir(projectDir) {
  return getProjectAgentsDir(projectDir);
}

export { getAgentsDir as g, writeAgentFile as w };
