import { spawn } from "node:child_process";
/**
 * Platform-aware spawn that centralizes Windows shell and quoting concerns.
 *
 * On Windows:
 * - Enables `shell: true` (routes through cmd.exe) unless the caller explicitly sets `shell`
 * - Quotes the command and arguments so paths with spaces survive cmd.exe parsing
 * - Always sets `windowsHide: true` to prevent console window flashes
 *
 * On other platforms the call is passed through to `spawn` unchanged (with `windowsHide: true`).
 */
export function spawnProcess(command, args, options) {
    const isWindows = process.platform === "win32";
    // Only route through cmd.exe when the caller hasn't opted out. Some callers
    // (e.g. Claude Agent SDK spawning) pass shell:false explicitly because their
    // argv contains double quotes that cmd.exe would mangle — in that case we
    // must invoke CreateProcess directly and not pre-quote the command/args.
    const useShell = options?.shell ?? isWindows;
    const resolvedCommand = isWindows && useShell ? quoteForCmd(command) : command;
    const resolvedArgs = isWindows && useShell ? args.map(quoteForCmd) : args;
    return spawn(resolvedCommand, resolvedArgs, {
        ...options,
        shell: useShell,
        windowsHide: true,
    });
}
/**
 * Quote a string for cmd.exe if it contains spaces and isn't already quoted.
 * No-op for strings without spaces or strings that are already double-quoted.
 */
function quoteForCmd(value) {
    if (!value.includes(" "))
        return value;
    if (value.startsWith('"') && value.endsWith('"'))
        return value;
    return `"${value}"`;
}
//# sourceMappingURL=spawn.js.map