/**
 * Persistence-only redaction for `.lisa/readiness.json`.
 * @module cli/doctor-readiness-report-redaction
 */
import { createHash } from "node:crypto";
import { CONTEXT_ROUTING_DIMENSION_ID } from "./doctor-readiness-context.js";
/** Evidence strings for B6 include source-doc excerpts; persist hashes instead. */
const DOC_EXCERPT_PATTERN = /: "([\s\S]*?)"(?=(?: \| `| \| \(\+\d|$))/g;
/**
 * Remove source-document prose excerpts from the persisted report while keeping
 * the in-memory blocker decision untouched. The console detail already names
 * the blocker; `.lisa/readiness.json` should not become a second committed copy
 * of potentially sensitive docs text.
 * @param report - Full readiness report computed for this run
 * @returns Report safe to serialize to `.lisa/readiness.json`
 */
export function sanitizeReadinessReportForPersistence(report) {
    return {
        ...report,
        blockers: report.blockers.map(blocker => blocker.id === "B6"
            ? {
                ...blocker,
                evidence: redactDocExcerpts(blocker.evidence),
            }
            : blocker),
        dimensions: report.dimensions.map(dimension => dimension.id === CONTEXT_ROUTING_DIMENSION_ID
            ? {
                ...dimension,
                findings: dimension.findings.map(sanitizeContextFinding),
            }
            : dimension),
    };
}
/**
 * Redact B6 finding strings that can contain quoted documentation excerpts.
 * @param value - One untyped finding
 * @returns Finding with prose excerpts redacted, or the original value
 */
function sanitizeContextFinding(value) {
    if (typeof value !== "object" || value === null || Array.isArray(value)) {
        return value;
    }
    return Object.fromEntries(Object.entries(value).map(([key, entry]) => [
        key,
        typeof entry === "string" ? redactDocExcerpts(entry) : entry,
    ]));
}
/**
 * Replace quoted documentation excerpts with a stable digest.
 * @param text - Evidence text
 * @returns Evidence text without the raw excerpt
 */
function redactDocExcerpts(text) {
    return text.replace(DOC_EXCERPT_PATTERN, (_match, excerpt) => {
        const digest = createHash("sha256").update(excerpt).digest("hex");
        return `: [doc excerpt redacted sha256:${digest}]`;
    });
}
//# sourceMappingURL=doctor-readiness-report-redaction.js.map