import os from "node:os";
import path from "node:path";
import { resolveAgentId } from "@poe-code/agent-defs";
const agentHookConfigs = {
    "claude-code": {
        globalHookPath: "~/.claude/settings.json",
        localHookPath: ".claude/settings.json",
        format: "claude-settings-json",
        supportedEvents: [
            "SessionStart",
            "SessionEnd",
            "UserPromptSubmit",
            "PreToolUse",
            "PostToolUse",
            "PermissionRequest",
            "Stop",
            "StopFailure",
            "Notification",
            "PreCompact",
            "PostCompact",
            "SubagentStart",
            "SubagentStop"
        ],
        supportedHandlerTypes: ["command", "http", "mcp_tool", "prompt", "agent"],
        placeholders: {
            projectDir: "${CLAUDE_PROJECT_DIR}",
            pluginRoot: "${CLAUDE_PLUGIN_ROOT}",
            pluginData: "${CLAUDE_PLUGIN_DATA}"
        }
    },
    codex: {
        globalHookPath: "~/.codex/hooks.json",
        localHookPath: ".codex/hooks.json",
        format: "codex-hooks-json",
        supportedEvents: [
            "SessionStart",
            "UserPromptSubmit",
            "PreToolUse",
            "PostToolUse",
            "PermissionRequest",
            "Stop"
        ],
        supportedHandlerTypes: ["command"],
        placeholders: {
            projectDir: "$(git rev-parse --show-toplevel)",
            pluginRoot: "$PLUGIN_ROOT",
            pluginData: "$PLUGIN_DATA"
        }
    }
};
export const supportedHookAgents = Object.freeze(Object.keys(agentHookConfigs));
export function resolveAgentSupport(input, registry = agentHookConfigs) {
    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: cloneAgentHookConfig(config) };
}
export function getAgentConfig(agentId) {
    const support = resolveAgentSupport(agentId);
    return support.status === "supported" ? support.config : undefined;
}
function cloneAgentHookConfig(config) {
    return {
        ...config,
        supportedEvents: [...config.supportedEvents],
        supportedHandlerTypes: [...config.supportedHandlerTypes],
        placeholders: { ...config.placeholders }
    };
}
function expandHome(targetPath, homeDir = os.homedir()) {
    if (!targetPath?.startsWith("~")) {
        return targetPath;
    }
    if (targetPath === "~") {
        return homeDir;
    }
    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 resolveHookPath(config, scope, cwd, homeDir) {
    if (scope === "global") {
        return path.resolve(expandHome(config.globalHookPath, homeDir));
    }
    return config.localHookPath ? path.resolve(cwd, config.localHookPath) : undefined;
}
