import { execFile } from "node:child_process";
import { runCodexCli } from "./cli-runners/codex.js";
import { runJsonCli } from "./cli-runners/json.js";
import { runOpenClawCli } from "./cli-runners/openclaw.js";
import { runOpenCodeCli } from "./cli-runners/opencode.js";
import { runPiCli } from "./cli-runners/pi.js";
import { runAgyCli, runCopilotCli } from "./cli-runners/plain.js";
const DEFAULT_BINARIES = {
    claude: "claude",
    codex: "codex",
    gemini: "gemini",
    agent: "agent",
    openclaw: "openclaw",
    opencode: "opencode",
    copilot: "copilot",
    agy: "agy",
    pi: "pi",
};
const PROVIDER_PATH_ENV = {
    claude: "CLAUDE_PATH",
    codex: "CODEX_PATH",
    gemini: "GEMINI_PATH",
    agent: "AGENT_PATH",
    openclaw: "OPENCLAW_PATH",
    opencode: "OPENCODE_PATH",
    copilot: "COPILOT_PATH",
    agy: "AGY_PATH",
    pi: "PI_PATH",
};
const isNonEmptyString = (value) => typeof value === "string" && value.trim().length > 0;
function getCliProviderConfig(provider, config) {
    if (!config)
        return undefined;
    return config[provider];
}
export function isCliDisabled(provider, config) {
    return Boolean(config && Array.isArray(config.enabled) && !config.enabled.includes(provider));
}
export function resolveCliBinary(provider, config, env) {
    const providerConfig = getCliProviderConfig(provider, config);
    if (isNonEmptyString(providerConfig?.binary))
        return providerConfig.binary.trim();
    const pathKey = PROVIDER_PATH_ENV[provider];
    if (isNonEmptyString(env[pathKey]))
        return env[pathKey].trim();
    const envKey = `SUMMARIZE_CLI_${provider.toUpperCase()}`;
    if (isNonEmptyString(env[envKey]))
        return env[envKey].trim();
    return DEFAULT_BINARIES[provider];
}
export async function runCliModel({ provider, prompt, model, allowTools, timeoutMs, env, execFileImpl, config, cwd, extraArgs, systemPrompt, signal, }) {
    const providerConfig = getCliProviderConfig(provider, config);
    const requestedModel = isNonEmptyString(model)
        ? model.trim()
        : isNonEmptyString(providerConfig?.model)
            ? providerConfig.model.trim()
            : null;
    const providerExtraArgs = [...(providerConfig?.extraArgs ?? []), ...(extraArgs ?? [])];
    const effectiveEnv = provider === "gemini" && !isNonEmptyString(env.GEMINI_CLI_NO_RELAUNCH)
        ? { ...env, GEMINI_CLI_NO_RELAUNCH: "true" }
        : env;
    const options = {
        binary: resolveCliBinary(provider, config, env),
        prompt,
        requestedModel,
        allowTools,
        timeoutMs,
        env: effectiveEnv,
        execFileImpl: execFileImpl ?? execFile,
        providerConfig,
        providerExtraArgs,
        cwd,
        systemPrompt,
        signal,
    };
    if (provider === "openclaw")
        return await runOpenClawCli(options);
    if (provider === "opencode")
        return await runOpenCodeCli(options);
    if (provider === "codex")
        return await runCodexCli(options);
    if (provider === "copilot")
        return await runCopilotCli(options);
    if (provider === "agy")
        return await runAgyCli(options);
    if (provider === "pi")
        return await runPiCli(options);
    return await runJsonCli(provider, options);
}
//# sourceMappingURL=cli.js.map