/**
 * Live-status probe for the console top-bar Lisa version chip.
 *
 * Reuses doctor `checkVersion` so `/api/status` and `lisa doctor` cannot drift.
 * @module cli/ui-lisa-version
 */
import { checkVersion, } from "./doctor.js";
import { runUpdateCheck } from "./update-check.js";
const LISA_VERSION_PROBE_ID = "lisa-version";
const INSTALLED_LATEST_DETAIL = /^Installed (.+); latest is (.+)$/u;
const LATEST_UNAVAILABLE_DETAIL = /^Latest version unavailable(?: \((.+)\))?$/u;
const OFFLINE_SKIP_DETAIL = "Skipped network check in offline mode";
const UNRECOGNIZED_MESSAGE = "Version check returned an unrecognized result";
/**
 * Default collaborator for the shared doctor version check.
 */
const DEFAULT_VERSION_PROBE_DEPS = {
    runUpdateCheck,
};
/**
 * Map a `checkVersion` doctor result onto the live-status tri-state contract.
 *
 * Doctor's offline skip returns status "ok" — that must become unknown here so
 * the top bar never shows a false green when latest was never fetched.
 * Latest-unavailable is also doctor "warn"; surface it as unknown, not amber.
 * @param check - Result from the shared doctor version check
 * @returns Probe result for `#healthChip`
 */
export function mapLisaVersionCheck(check) {
    if (check.detail === OFFLINE_SKIP_DETAIL) {
        return {
            state: "unknown",
            reason: "offline-skipped",
            message: check.detail,
        };
    }
    const unavailable = LATEST_UNAVAILABLE_DETAIL.exec(check.detail);
    if (unavailable !== null) {
        return {
            state: "unknown",
            reason: unavailable[1] ?? "latest-unavailable",
            message: check.detail,
        };
    }
    const installed = INSTALLED_LATEST_DETAIL.exec(check.detail);
    if (installed !== null &&
        (check.status === "ok" || check.status === "warn")) {
        return {
            state: "value",
            value: {
                current: installed[1] ?? "",
                latest: installed[2] ?? "",
                outdated: check.status === "warn",
            },
        };
    }
    return {
        state: "unknown",
        reason: "unrecognized-version-check",
        message: check.detail.trim().length > 0 ? check.detail : UNRECOGNIZED_MESSAGE,
    };
}
/**
 * Create the probe that reports whether the installed Lisa matches npm latest.
 *
 * Always calls `checkVersion(deps, false)` — never offline:true, which would
 * produce doctor's false-green "Skipped network check in offline mode" ok.
 * @param dependencies - Injectable doctor collaborators for focused tests
 * @returns Probe emitting current/latest/outdated or an honest unknown
 */
export function createLisaVersionProbe(dependencies = {}) {
    const deps = { ...DEFAULT_VERSION_PROBE_DEPS, ...dependencies };
    return {
        id: LISA_VERSION_PROBE_ID,
        timeoutMs: 5_000,
        run: async () => {
            const check = await checkVersion(deps, false);
            return mapLisaVersionCheck(check);
        },
    };
}
//# sourceMappingURL=ui-lisa-version.js.map