import { appendFileSync, mkdirSync } from "node:fs";
import { dirname, isAbsolute, join, resolve } from "node:path";
function isEnabled(value) {
    const normalized = value?.trim().toLowerCase();
    return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
}
function isShellDebugEnabled() {
    return (isEnabled(process.env.BEST_AGENT_SHELL_DEBUG) ||
        isEnabled(process.env.BEST_AGENT_DEBUG) ||
        isEnabled(process.env.DIMCODE_DEBUG));
}
function shellDebugLogPath(cwd) {
    const explicit = process.env.BEST_AGENT_SHELL_DEBUG_LOG?.trim() || process.env.DIMCODE_DEBUG_LOG?.trim();
    if (explicit) {
        return isAbsolute(explicit) ? explicit : resolve(cwd, explicit);
    }
    return join(cwd, "logs", "shell-tool-debug.log");
}
export function writeShellDebugLog(cwd, eventName, payload) {
    if (!isShellDebugEnabled()) {
        return;
    }
    try {
        const filePath = shellDebugLogPath(cwd);
        mkdirSync(dirname(filePath), { recursive: true });
        const timestamp = new Date().toISOString();
        const suffix = payload === undefined ? "" : ` ${JSON.stringify(payload)}`;
        appendFileSync(filePath, `[${timestamp}] ${eventName}${suffix}\n`);
    }
    catch {
        // Debug logging must not affect tool execution.
    }
}
//# sourceMappingURL=debug-log.js.map