import os from "node:os";
import path from "node:path";
import { resolveAgentId } from "@poe-code/agent-defs";
const agentSkillConfigs = {
    "claude-code": {
        globalSkillDir: "~/.claude/skills",
        localSkillDir: ".claude/skills"
    },
    codex: {
        globalSkillDir: "~/.codex/skills",
        localSkillDir: ".codex/skills"
    },
    cursor: {
        globalSkillDir: "~/.cursor/skills-cursor",
        localSkillDir: ".cursor/skills"
    },
    "gemini-cli": {
        globalSkillDir: "~/.gemini/skills",
        localSkillDir: ".gemini/skills"
    },
    opencode: {
        globalSkillDir: "~/.config/opencode/skills",
        localSkillDir: ".opencode/skills"
    },
    goose: {
        globalSkillDir: "~/.agents/skills",
        localSkillDir: ".agents/skills"
    }
};
export const supportedAgents = Object.freeze(Object.keys(agentSkillConfigs));
export function resolveAgentSupport(input, registry = agentSkillConfigs) {
    const resolvedId = resolveAgentId(input);
    if (!resolvedId) {
        return { status: "unknown", input };
    }
    const config = registry[resolvedId];
    if (!config) {
        return { status: "unsupported", input, id: resolvedId };
    }
    return { status: "supported", input, id: resolvedId, config: { ...config } };
}
export function getAgentConfig(agentId) {
    const support = resolveAgentSupport(agentId);
    return support.status === "supported" ? support.config : undefined;
}
function expandHome(targetPath, homeDir = os.homedir()) {
    if (!targetPath?.startsWith("~")) {
        return targetPath;
    }
    if (targetPath === "~") {
        return homeDir;
    }
    // Handle ~./ -> ~/.
    if (targetPath.startsWith("~./")) {
        targetPath = `~/.${targetPath.slice(3)}`;
    }
    let remainder = targetPath.slice(1);
    if (remainder.startsWith("/") || remainder.startsWith("\\")) {
        remainder = remainder.slice(1);
    }
    else if (remainder.startsWith(".")) {
        remainder = remainder.slice(1);
        if (remainder.startsWith("/") || remainder.startsWith("\\")) {
            remainder = remainder.slice(1);
        }
    }
    return remainder.length === 0 ? homeDir : path.join(homeDir, remainder);
}
export function resolveSkillDir(config, scope, cwd, homeDir) {
    if (scope === "global") {
        return path.resolve(expandHome(config.globalSkillDir, homeDir));
    }
    return path.resolve(cwd, config.localSkillDir);
}
