import { commandMatches } from "../types/command.js";
import { buildCommandCatalog } from "./catalog.js";
const COMMAND_ALIASES = {
    "add-dir": "add-dir",
    "agents-platform": "agents",
    "ant-trace": "trace",
    "autofix-pr": "review",
    "backfill-sessions": "sessions",
    "break-cache": "cache",
    "bughunter": "review",
    "chrome": "browser",
    "color": "theme",
    "ctx_viz": "context",
    "debug-tool-call": "trace",
    "desktop": "status",
    "env": "config",
    "exit": "quit",
    "extra-usage": "usage",
    "fix": "review",
    "help-v2": "help",
    "keybindings": "settings",
    "memory": "memory",
    "migrate-installer": "doctor",
    "migrate": "doctor",
    "model": "model",
    "mcp": "mcp",
    "permissions": "permissions",
    "plugin": "plugins",
    "plugins": "plugins",
    "pr": "review",
    "release-notes": "version",
    "review": "review",
    "security-review": "security-review",
    "statusline": "status",
    "sync": "status",
    "terminal-setup": "doctor",
    "theme": "theme",
    "ultraplan": "plan",
};
export function createCommandAdapter(referenceName, targetName = commandTargetName(referenceName)) {
    return {
        name: normalizeCommandName(referenceName),
        description: `ClaudeCode path-compatible adapter for DeepSeekCode /${targetName}.`,
        usage: "[args]",
        async execute(args, context) {
            const target = findLocalCommand(targetName, context);
            if (target)
                return target.execute(args, context);
            return {
                message: [
                    `/${normalizeCommandName(referenceName)} is available as a ClaudeCode-compatible source path.`,
                    `DeepSeekCode has not implemented the /${targetName} command yet.`,
                    "Run /help to see supported local commands, or use /plugins and /skills for extension workflows.",
                ].join("\n"),
            };
        },
    };
}
export function commandAdapterInfo(referenceName, context) {
    const targetName = commandTargetName(referenceName);
    return {
        referenceName: normalizeCommandName(referenceName),
        targetName,
        implemented: context ? Boolean(findLocalCommand(targetName, context)) : Boolean(COMMAND_ALIASES[normalizeCommandName(referenceName)]),
    };
}
export function commandTargetName(referenceName) {
    const normalized = normalizeCommandName(referenceName);
    return COMMAND_ALIASES[normalized] ?? normalized;
}
export function normalizeCommandName(value) {
    return value
        .replace(/\\/g, "/")
        .split("/")
        .filter(Boolean)[0]
        ?.replace(/\.(tsx?|jsx?)$/, "")
        .toLowerCase() ?? value.toLowerCase();
}
function findLocalCommand(name, context) {
    return buildCommandCatalog(context).find((command) => commandMatches(command, name));
}
