// wedge — pure brew-name resolution, peer of apt-resolve.ts.
//
// Same shape as the apt-resolve.ts: pure decision function, explicit
// input record, explicit output record, no spawnSync, no fs reads. The
// installer wraps this with the actual `brew list --versions` and `brew
// install` calls; this module only decides which name
// to feed to those calls and which diagnostic line to log when an alias is
// applied.
//
// Brew's decision space is narrower than apt's — there is no apt-cache-policy
// analogue (Homebrew tells you `brew install` failed, never that a package
// is virtual), so the resolver collapses to two branches: alias-hit or
// pass-through. The brewInstalled gate short-circuits both: if the package
// is already on disk, the name is concrete and we return it unchanged.
// Virtual-package aliases applied when an apt-side name (used in shared
// installer call sites) needs to map to a brew formula name. Keyed by the
// apt name the installer passes in, valued by the brew formula name. Add
// entries here when a new shared name surfaces — every entry gets a
// deliberate test grid case (see brew-resolve.test.ts shape lock).
export const DARWIN_ALIASES = {
    // Debian/Ubuntu ship Node.js as `nodejs`; Homebrew's pinned version is
    // `node@22` (current Maxy floor — see pinned-binaries.ts). Mapping here
    // lets follow-up tasks pass `nodejs` from the shared dep list and have
    // the brew backend translate transparently.
    nodejs: "node@22",
};
/**
 * Parse the stdout of `brew list --versions <pkg>` into the version tokens
 * after the package name. Output shape:
 *
 *   "node@22 22.18.1 22.18.0\n"  →  ["22.18.1", "22.18.0"]
 *   "cloudflared 2025.1.4\n"     →  ["2025.1.4"]
 *   "node@22\n"                  →  []   (cellar empty between uninstall + cleanup)
 *   ""                           →  []   (pkg missing — brew exited 1)
 *   "   \n"                      →  []   (whitespace-only — never a real version)
 *
 * The first whitespace-separated token is always the package name and is
 * dropped. Caller filters by version-shape if needed; this resolver returns
 * everything after the name unfiltered.
 */
export function parseBrewListVersions(stdout) {
    const tokens = stdout.trim().split(/\s+/).filter((t) => t.length > 0);
    if (tokens.length < 2)
        return [];
    return tokens.slice(1);
}
/**
 * Pure decision: given the brew-installed fact, decide which package name to
 * use. Resolution order:
 *   1. brewInstalled true → name is concrete; return pkg unchanged, no log.
 *   2. pkg is in DARWIN_ALIASES → return the alias and emit the log line.
 *   3. Otherwise return pkg unchanged — caller's post-check will throw,
 *      preserving the apt-resolve.ts fail-loud contract for genuinely missing
 *      packages.
 */
export function decideBrewResolution(input) {
    const { pkg, brewInstalled } = input;
    if (brewInstalled)
        return { resolved: pkg, log: null };
    if (Object.prototype.hasOwnProperty.call(DARWIN_ALIASES, pkg)) {
        const alias = DARWIN_ALIASES[pkg];
        return {
            resolved: alias,
            log: `  brew-resolve ${pkg} → ${alias} (reason=darwin-alias)`,
        };
    }
    return { resolved: pkg, log: null };
}
