// pure apt-name resolution, extracted from index.ts so a unit
// test can exercise every branch without spawning real apt/dpkg or reading
// /etc/os-release. Mirrors the port-resolution.ts pattern:
// inputs in, decision out, no I/O. The installer wraps this with the actual
// spawnSync calls and side-effecting logFile.
//
// The original logic landed (chromium-noble alias). This file
// preserves that semantics verbatim — every branch maps 1:1.
// Virtual-package aliases applied when `apt-cache policy <pkg>` returns
// `Candidate: (none)`. Keyed by apt-level alias, valued by the real .deb
// name dpkg records post-install. Scoped per distro family by the caller's
// `ubuntuLike` input. Add entries here if a new alias class surfaces.
export const UBUNTU_ALIASES = {
    // Noble ships `chromium` as a virtual package that resolves to
    // `chromium-browser`, which is itself a transitional stub delegating to
    // the snap. The runtime binary at /usr/bin/chromium is a snap symlink;
    // dpkg records `chromium-browser` post-install, never `chromium`.
    chromium: "chromium-browser",
};
/** Parse `/etc/os-release` content into a key→value map. */
export function parseOsRelease(raw) {
    const out = {};
    for (const line of raw.split("\n")) {
        const m = line.match(/^([A-Z_]+)=(.*)$/);
        if (m)
            out[m[1]] = m[2].replace(/^"|"$/g, "");
    }
    return out;
}
/** True when the host is Ubuntu or Ubuntu-derivative (Pop!_OS, Mint, etc.). */
export function isUbuntuLike(os) {
    if (os.ID === "ubuntu")
        return true;
    const likes = (os.ID_LIKE ?? "").split(/\s+/);
    return likes.includes("ubuntu");
}
/**
 * Extract the Candidate field from `apt-cache policy <pkg>` stdout. Returns
 * the literal string after `Candidate:` (so `(none)` stays as `(none)` —
 * meaningful to the resolver, not a magic value). Returns null when no
 * Candidate line is present (spawn failure, malformed output, etc.).
 */
export function parseAptCacheCandidate(stdout) {
    const m = stdout.match(/Candidate:\s*(\S+)/);
    return m ? m[1] : null;
}
/**
 * Pure decision: given the dpkg/apt/os-release facts, decide which package
 * name to use. Resolution order:
 *   1. dpkg -s pkg succeeded → name is concrete and installed; return pkg.
 *   2. apt-cache policy reports a real Candidate (not `(none)`) → return pkg.
 *   3. Host is Ubuntu-like AND pkg is in UBUNTU_ALIASES → return the alias
 *      and emit the log line.
 *   4. Otherwise return pkg unchanged — caller's post-check will throw,
 *      preserving the fail-loud contract for genuinely missing
 *      packages.
 */
export function decideAptResolution(input) {
    const { pkg, dpkgInstalled, aptCandidate, ubuntuLike, distro } = input;
    if (dpkgInstalled)
        return { resolved: pkg, log: null };
    if (aptCandidate && aptCandidate !== "(none)") {
        return { resolved: pkg, log: null };
    }
    if (ubuntuLike && Object.prototype.hasOwnProperty.call(UBUNTU_ALIASES, pkg)) {
        const alias = UBUNTU_ALIASES[pkg];
        return {
            resolved: alias,
            log: `  apt-resolve ${pkg} → ${alias} (reason=candidate-none, distro=${distro})`,
        };
    }
    return { resolved: pkg, log: null };
}
