import fss from "node:fs";
import path from "node:path";
import { resolveProjectConfig } from "../../knowledge/loader.js";
import { generateHooksConfig } from "../../adapters/claude_code/hooks.js";
import { generateClaudeMd } from "../../adapters/claude_code/hooks.js";
import { generateTraeRules } from "../../adapters/trae/trae_rules.js";
import { generateTraeMcpConfig } from "../../adapters/trae/trae_config.js";
import { generateCodexMcpConfig, generateCodexHooksConfig } from "../../adapters/codex/codex_config.js";
import { generateCodexAgentsMd } from "../../adapters/codex/codex_rules.js";
import { userInfo } from "../../engine/core/logger.js";
import { safeJsonParse } from "../../engine/core/helpers.js";
export async function cmdGenerateHooks(projectPath) {
    const targetPath = projectPath ?? process.cwd();
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- MCP SDK 桥接/动态 JSON 解析，字段由运行时决定
    const hooksConfig = generateHooksConfig();
    const claudeDir = path.join(targetPath, ".claude");
    fss.mkdirSync(claudeDir, { recursive: true });
    const settingsPath = path.join(claudeDir, "settings.json");
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- MCP SDK 桥接/动态 JSON 解析，字段由运行时决定
    let existing = {};
    if (fss.existsSync(settingsPath)) {
        try {
            existing = safeJsonParse(fss.readFileSync(settingsPath, "utf-8")) ?? {};
        }
        catch (e) {
            userInfo("  ⚠️ settings.json 解析失败，跳过更新（保留原文件）: " + String(e));
            return;
        }
    }
    // 仅 merge hooks 字段，保留用户的 permissions/env 等设置
    existing.hooks = hooksConfig.hooks;
    fss.writeFileSync(settingsPath, JSON.stringify(existing, null, 2), "utf-8");
    userInfo("✅ .claude/settings.json 已更新 hooks 配置");
}
export async function cmdGenerateClaudeMd() {
    await cmdGenerateClaudeMdInner(process.cwd());
}
export async function cmdGenerateTrae() {
    await cmdGenerateTraeInner(process.cwd());
}
export async function cmdGenerateCodex() {
    await cmdGenerateCodexInner(process.cwd());
}
export async function cmdGenerateCodexInner(projectPath) {
    // 生成 .codex/config.toml
    const codexDir = path.join(projectPath, ".codex");
    fss.mkdirSync(codexDir, { recursive: true });
    const tomlContent = generateCodexMcpConfig(projectPath);
    fss.writeFileSync(path.join(codexDir, "config.toml"), tomlContent, "utf-8");
    userInfo("✅ .codex/config.toml 已创建");
    // 生成 .codex/hooks.json
    const hooksContent = generateCodexHooksConfig();
    fss.writeFileSync(path.join(codexDir, "hooks.json"), hooksContent, "utf-8");
    userInfo("✅ .codex/hooks.json 已创建");
    // 生成 AGENTS.md
    const { config } = await resolveProjectConfig(projectPath);
    const agentsContent = generateCodexAgentsMd(config);
    fss.writeFileSync(path.join(projectPath, "AGENTS.md"), agentsContent, "utf-8");
    userInfo("✅ AGENTS.md 已创建");
}
export async function cmdGenerateTraeInner(projectPath) {
    // 生成 .trae/mcp.json
    const traeDir = path.join(projectPath, ".trae");
    fss.mkdirSync(traeDir, { recursive: true });
    const traeMcpPath = path.join(traeDir, "mcp.json");
    const traeMcpConfig = generateTraeMcpConfig(projectPath);
    fss.writeFileSync(traeMcpPath, JSON.stringify(traeMcpConfig, null, 2), "utf-8");
    userInfo("✅ .trae/mcp.json created");
    // 生成 .trae/rules/project_rules.md
    const rulesDir = path.join(traeDir, "rules");
    fss.mkdirSync(rulesDir, { recursive: true });
    const { config } = await resolveProjectConfig(projectPath);
    const rulesContent = generateTraeRules(config);
    fss.writeFileSync(path.join(rulesDir, "project_rules.md"), rulesContent, "utf-8");
    userInfo("✅ .trae/rules/project_rules.md created");
}
export async function cmdGenerateClaudeMdInner(projectPath) {
    const { config } = await resolveProjectConfig(projectPath);
    const content = generateClaudeMd(config);
    fss.writeFileSync(path.join(projectPath, "CLAUDE.md"), content, "utf-8");
    userInfo("✅ CLAUDE.md created");
}
//# sourceMappingURL=hooks.js.map