import { existsSync } from "node:fs";
import path from "node:path";
/**
 * Fixed Git-Bash install locations to probe on Windows, in priority order.
 * These cover the official Git for Windows installer (system + per-user) plus
 * the common 32-bit path. We NEVER include `System32\bash.exe` (WSL).
 */
function gitBashFixedCandidates(env) {
    const programFiles = env.ProgramFiles ?? "C:\\Program Files";
    const programFilesX86 = env["ProgramFiles(x86)"] ?? "C:\\Program Files (x86)";
    const localAppData = env.LOCALAPPDATA;
    const candidates = [
        path.win32.join(programFiles, "Git", "bin", "bash.exe"),
        path.win32.join(programFilesX86, "Git", "bin", "bash.exe"),
    ];
    if (localAppData) {
        candidates.push(path.win32.join(localAppData, "Programs", "Git", "bin", "bash.exe"));
    }
    return candidates;
}
/**
 * Derive Git Bash from `git.exe` on PATH: Git for Windows ships `git.exe` under
 * `<root>\cmd` (and `<root>\bin`), with `bash.exe` at `<root>\bin\bash.exe`.
 */
function gitBashFromPath(env, exists) {
    const rawPath = env.PATH ?? env.Path ?? "";
    if (!rawPath)
        return null;
    // Windows PATH is always ';'-separated. (Don't use path.delimiter — it
    // reflects the HOST OS, which breaks when this win32-only branch is exercised
    // in cross-platform tests on a POSIX host.)
    for (const dir of rawPath.split(";")) {
        if (!dir)
            continue;
        const gitExe = path.win32.join(dir, "git.exe");
        if (!exists(gitExe))
            continue;
        // <root>\cmd\git.exe or <root>\bin\git.exe → <root>\bin\bash.exe
        const gitRoot = path.win32.dirname(path.win32.dirname(gitExe));
        const bash = path.win32.join(gitRoot, "bin", "bash.exe");
        if (exists(bash))
            return bash;
    }
    return null;
}
/** Locate a usable Git Bash on Windows, or null if none is installed. */
function findGitBash(env, exists) {
    for (const candidate of gitBashFixedCandidates(env)) {
        if (exists(candidate))
            return candidate;
    }
    return gitBashFromPath(env, exists);
}
/**
 * Resolve the shell executable + args to run a single command string.
 *
 * `GG_BASH` env override always wins (point it at any POSIX bash, e.g. a custom
 * Git Bash or msys2 install). Otherwise: non-Windows → `bash`; Windows → Git
 * Bash if found, else `cmd.exe`.
 */
export function resolveShell(command, opts = {}) {
    const platform = opts.platform ?? process.platform;
    const env = opts.env ?? process.env;
    const exists = opts.exists ?? existsSync;
    // Explicit override (power users / tests): treat as a POSIX bash.
    const override = env.GG_BASH?.trim();
    if (override) {
        return { file: override, args: ["-c", command], isCmdFallback: false };
    }
    if (platform !== "win32") {
        return { file: "bash", args: ["-c", command], isCmdFallback: false };
    }
    const gitBash = findGitBash(env, exists);
    if (gitBash) {
        return { file: gitBash, args: ["-c", command], isCmdFallback: false };
    }
    // Last resort: cmd.exe. `/d` skips AutoRun, `/s` + `/c` runs the rest as a
    // single command verbatim (correct quoting for an arbitrary command string).
    const comspec = env.ComSpec ?? env.COMSPEC ?? "C:\\Windows\\System32\\cmd.exe";
    return { file: comspec, args: ["/d", "/s", "/c", command], isCmdFallback: true };
}
//# sourceMappingURL=shell.js.map