import path from 'node:path';
import { exists, packageRoot, runProcess, which } from './fsx.js';
import { forceGpt55CodexArgs } from './codex-model-guard.js';
import { managedProxyEnvForChild } from './codex/managed-proxy-env.js';
export async function findCodexBinary() {
    const env = process.env.SKS_CODEX_BIN || process.env.DCODEX_CODEX_BIN || process.env.CODEX_BIN;
    if (env && await exists(env))
        return env;
    const global = await which('codex');
    if (global)
        return global;
    const root = packageRoot();
    const local = path.join(root, 'node_modules', '.bin', process.platform === 'win32' ? 'codex.cmd' : 'codex');
    if (await exists(local))
        return local;
    return null;
}
export async function codexVersion(bin) {
    if (!bin)
        return null;
    const result = await runProcess(bin, ['--version'], { timeoutMs: 10000, maxOutputBytes: 16 * 1024 });
    const text = `${result.stdout}${result.stderr}`.trim();
    return result.code === 0 ? text : null;
}
export const EMPTY_CODEX_INFO = Object.freeze({
    bin: null,
    version: null,
    available: false
});
export async function getCodexInfo() {
    const bin = await findCodexBinary();
    const version = await codexVersion(bin);
    return { bin, version, available: Boolean(bin) };
}
export function buildCodexExecArgs({ root, prompt, outputFile, json = true, profile = null, extraArgs = [] }) {
    const args = ['exec', '--cd', root];
    if (profile)
        args.push('--profile', profile);
    if (json)
        args.push('--json');
    if (outputFile)
        args.push('--output-last-message', outputFile);
    args.push(...forceGpt55CodexArgs(extraArgs));
    args.push(prompt);
    return args;
}
export async function runCodexExec({ root, prompt, outputFile, json = true, profile = null, extraArgs = [], onStdout, onStderr, logDir = null, stdoutFile = null, stderrFile = null, maxBufferBytes = 256 * 1024, timeoutMs = null }) {
    const bin = await findCodexBinary();
    if (!bin) {
        return {
            code: 127,
            stdout: '',
            stderr: 'Codex CLI not found. Install @openai/codex or set SKS_CODEX_BIN.',
            stdoutBytes: 0,
            stderrBytes: 0,
            truncated: false,
            timedOut: false
        };
    }
    const args = buildCodexExecArgs({ root, prompt, outputFile, json, profile, extraArgs });
    const effectiveTimeoutMs = Number(timeoutMs || process.env.SKS_CODEX_TIMEOUT_MS || process.env.DCODEX_CODEX_TIMEOUT_MS || 30 * 60 * 1000);
    return runProcess(bin, args, {
        cwd: root,
        env: managedProxyEnvForChild(process.env),
        onStdout,
        onStderr,
        timeoutMs: effectiveTimeoutMs,
        maxOutputBytes: maxBufferBytes,
        stdoutFile: stdoutFile || (logDir ? path.join(logDir, 'codex.stdout.log') : undefined),
        stderrFile: stderrFile || (logDir ? path.join(logDir, 'codex.stderr.log') : undefined)
    });
}
//# sourceMappingURL=codex-adapter.js.map