// Interpretation layer for the diagnostic dump.
//
// Pure functions over the already-collected DebugData (no I/O, no probing —
// the data sections did that). Each rule turns a data condition into a
// human-readable Finding with, where possible, the exact fix command. This
// is what makes the dump self-service for support: instead of "Chrome NOT
// AVAILABLE", the user sees "→ run `doc-detective install runtime`".
//
// Modeled on the hints philosophy in src/hints/: small, pure, concrete.
// `import type` keeps this a compile-time-only dependency on index.ts, so
// there's no runtime import cycle.
function findDriver(data, pkg) {
    return (data.appium?.drivers || []).find((d) => d.name === pkg);
}
function findBrowser(data, name) {
    return (data.browsers?.browsers || []).find((b) => b.name === name);
}
// Chrome can't run and its Appium driver is missing or unregistered → the
// runtime install is incomplete.
const chromeUnavailable = (data) => {
    const chrome = findBrowser(data, "chrome");
    if (!chrome || !chrome.supported || chrome.available)
        return null;
    const driver = findDriver(data, "appium-chromium-driver");
    // Suppress only when the driver is confirmed resolvable AND registered.
    if (driver && driver.npmResolvable && driver.registered === true)
        return null;
    // Tailor the detail to what we actually know: a missing driver, a confirmed
    // unregistered one, and an unknown-registration one (manifest absent) are
    // distinct — saying "missing or not registered" for the unknown case would
    // be wrong (the package resolved fine; only its registration is unverified).
    const detail = !driver || !driver.npmResolvable
        ? "Chrome shows NOT AVAILABLE and its Appium driver (appium-chromium-driver) is missing, so browser tests can't run."
        : driver.registered === false
            ? "Chrome shows NOT AVAILABLE and appium-chromium-driver is installed but not registered under the active APPIUM_HOME."
            : "Chrome shows NOT AVAILABLE. appium-chromium-driver is installed, but its registration is unknown (Appium manifest missing or unreadable).";
    return {
        severity: "error",
        title: "Chrome is not available",
        detail,
        fix: "doc-detective install runtime",
    };
};
// Recorded install is older than the declared constraint, or the
// doc-detective / doc-detective-common versions disagree → stale install.
const staleInstall = (data) => {
    const outdated = (data.install?.rows || []).filter((r) => r.outdated);
    const lockstep = Boolean(data.docDetective?.lockstepWarning);
    if (outdated.length === 0 && !lockstep)
        return null;
    const reasons = [];
    if (outdated.length > 0) {
        reasons.push(`outdated: ${outdated.map((r) => r.assetId).join(", ")}`);
    }
    if (lockstep)
        reasons.push("doc-detective / doc-detective-common version mismatch");
    return {
        severity: "warning",
        title: "Install looks stale",
        detail: `${reasons.join("; ")}. A stale or mismatched install is a common source of hard-to-explain failures.`,
        fix: "doc-detective install runtime",
    };
};
// cacheDir not writable → lazy installs and recordings have nowhere to go.
const cacheNotWritable = (data) => {
    const entry = (data.cache?.entries || []).find((e) => e.label === "cacheDir");
    if (!entry || entry.writable !== false)
        return null;
    return {
        severity: "error",
        title: "Cache directory is not writable",
        detail: `cacheDir (${entry.path}) is not writable, so doc-detective can't lazy-install browsers or runtime packages there.`,
        fix: "set DOC_DETECTIVE_CACHE_DIR to a writable path",
    };
};
// A proxy is configured and something the installer needs is missing — flag
// the proxy as a likely lazy-install blocker (we don't probe reachability;
// that's the opt-in --check-network work).
const proxyMaybeBlocking = (data) => {
    // Catch both the standard proxy env vars (HTTP(S)_PROXY / ALL_PROXY) and a
    // proxy configured purely through npm (`npm config set proxy=…`), which
    // surfaces in the network section as npm_config_proxy / npm_config_https_proxy.
    const hasProxy = (data.network?.variables || []).some((v) => /^(https?_proxy|all_proxy)$/i.test(v.name) ||
        /^npm_config_(https?_)?proxy$/i.test(v.name));
    if (!hasProxy)
        return null;
    const missingNpm = (data.install?.rows || []).some((r) => r.kind === "npm" && !r.installed);
    if (!missingNpm)
        return null;
    return {
        severity: "info",
        title: "Proxy configured with missing runtime packages",
        detail: "A proxy is set and some runtime npm packages are not installed. If lazy installs fail, the proxy/registry is the likely cause — verify it's reachable and that proxy-agent is installed for @puppeteer/browsers downloads.",
    };
};
// Driver resolves from npm but Appium doesn't list it → APPIUM_HOME mismatch.
// `registered === false` is the only firing case: `null` (manifest unread)
// means registration is unknown, not a finding — a missing manifest must not
// false-flag every resolvable driver.
const driverNotRegistered = (data) => {
    const stranded = (data.appium?.drivers || []).filter((d) => d.npmResolvable && d.registered === false);
    if (stranded.length === 0)
        return null;
    return {
        severity: "warning",
        title: "Appium driver installed but not registered",
        detail: `${stranded
            .map((d) => d.name)
            .join(", ")} resolve from npm but Appium doesn't list them under the active APPIUM_HOME — usually an APPIUM_HOME pointing at the wrong node_modules.`,
        fix: "doc-detective install runtime",
    };
};
const RULES = [
    chromeUnavailable,
    staleInstall,
    cacheNotWritable,
    proxyMaybeBlocking,
    driverNotRegistered,
];
export function computeFindings(data) {
    const findings = [];
    for (const rule of RULES) {
        try {
            const finding = rule(data);
            if (finding)
                findings.push(finding);
        }
        catch {
            // A rule must never crash the dump; skip on error.
        }
    }
    return findings;
}
//# sourceMappingURL=findings.js.map