"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.planHistoricalCompatibilityDispositionSync = planHistoricalCompatibilityDispositionSync;
const node_crypto_1 = require("node:crypto");
const mutation_preconditions_1 = require("../filesystem/mutation-preconditions");
const canonical_write_target_1 = require("../paths/canonical-write-target");
const historical_compatibility_disposition_1 = require("./historical-compatibility-disposition");
const legacy_compatibility_projection_1 = require("./legacy-compatibility-projection");
function planHistoricalCompatibilityDispositionSync(options) {
    const projection = (0, legacy_compatibility_projection_1.projectLegacyPlanningCompatibility)({
        repositoryRoot: options.repositoryRoot,
        planningRootPath: options.planningRootPath,
        historicalCompatibilityDisposition: "ignore"
    });
    if (!projection.discovery.complete) {
        throw new TypeError("Historical compatibility disposition requires complete planning discovery.");
    }
    const supportedCodes = new Set(historical_compatibility_disposition_1.HISTORICAL_COMPATIBILITY_DISPOSITION_CODES);
    const gaps = projection.workUnits
        .flatMap((unit) => unit.evidenceGaps)
        .filter((gap) => supportedCodes.has(gap.code)
        && gap.paths.length > 0
        && gap.paths.every((repositoryPath) => repositoryPath.startsWith(".day-shift/planning/governed/")));
    const gapCounts = {};
    const entries = [];
    for (const gap of gaps) {
        gapCounts[gap.code] = (gapCounts[gap.code] ?? 0) + 1;
        for (const repositoryPath of gap.paths) {
            const target = (0, canonical_write_target_1.resolveCanonicalWriteTargetSync)(options.repositoryRoot, repositoryPath);
            const stats = target.exists
                ? (0, mutation_preconditions_1.computeContentRevisionSync)(target.runtimePath)
                : null;
            if (stats === null) {
                throw new TypeError(`Historical compatibility target '${repositoryPath}' is missing.`);
            }
            entries.push({
                code: gap.code,
                path: repositoryPath,
                contentRevision: stats
            });
        }
    }
    const encoded = (0, historical_compatibility_disposition_1.encodeHistoricalCompatibilityDisposition)({
        entries,
        createdAt: options.createdAt,
        createdBy: options.createdBy,
        reason: options.reason,
        gapCounts
    });
    return {
        protocolVersion: 1,
        readOnly: true,
        writeApplied: false,
        dispositionPath: historical_compatibility_disposition_1.HISTORICAL_COMPATIBILITY_DISPOSITION_PATH,
        dispositionId: encoded.document.dispositionId,
        dispositionDigest: `sha256:${(0, node_crypto_1.createHash)("sha256").update(encoded.contents).digest("hex")}`,
        entryCount: encoded.document.entryCount,
        uniquePathCount: encoded.document.uniquePathCount,
        gapOccurrenceCount: gaps.length,
        gapCounts: encoded.document.gapCounts,
        contents: encoded.contents
    };
}
