const WINDOWS_OPENCODE_COMMANDS = ["opencode.cmd", "opencode.exe", "opencode.bat", "opencode"];
const POSIX_OPENCODE_COMMANDS = ["opencode"];
export function openCodeCommandCandidates(options = {}) {
    const env = options.env ?? process.env;
    const platform = options.platform ?? process.platform;
    const configured = env.SEED_OPENCODE_BIN ?? env.OPENCODE_BIN;
    const defaults = platform === "win32" ? WINDOWS_OPENCODE_COMMANDS : POSIX_OPENCODE_COMMANDS;
    return unique([configured, ...defaults].filter(Boolean));
}
export function shouldUseShellForOpenCodeCommand(command, platform = process.platform) {
    return platform === "win32" && /\.(cmd|bat)$/i.test(command);
}
export function runOpenCodeSync(spawn, args, options, commandOptions = {}) {
    const platform = commandOptions.platform ?? process.platform;
    let lastResult;
    for (const command of openCodeCommandCandidates(commandOptions)) {
        const result = spawn(command, args, {
            ...options,
            shell: options.shell ?? shouldUseShellForOpenCodeCommand(command, platform),
        });
        const tagged = Object.assign(result, { command });
        if (!isMissingExecutable(result))
            return tagged;
        lastResult = tagged;
    }
    return lastResult ?? Object.assign(spawn("opencode", args, options), { command: "opencode" });
}
export function isMissingExecutable(result) {
    const code = result.error?.code;
    if (code === "ENOENT" || code === "EACCES")
        return true;
    const output = `${String(result.stderr ?? "")}\n${String(result.stdout ?? "")}`;
    return /is not recognized as an internal or external command/i.test(output);
}
function unique(values) {
    return [...new Set(values)];
}
//# sourceMappingURL=opencode-command.js.map