"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readStructuralPressureReport = readStructuralPressureReport;
const node_fs_1 = __importDefault(require("node:fs"));
const paths_1 = require("../../paths");
const implementation_summary_reconciliation_1 = require("../implementation-summary-reconciliation");
const contracts_1 = require("./contracts");
const scan_1 = require("./scan");
const state_1 = require("./state");
function readStructuralPressureReport(repositoryRoot, options) {
    const exclusions = options.exclusions ?? (0, scan_1.resolveStructuralPressureScanExclusions)();
    const stateAbsolutePath = (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, contracts_1.STRUCTURAL_PRESSURE_STATE_PATH);
    if (!node_fs_1.default.existsSync(stateAbsolutePath)) {
        const scan = (0, scan_1.scanStructuralPressureSources)(repositoryRoot, {
            scanPath: options.scanPath,
            thresholds: options.thresholds,
            entries: [],
            exclusions
        });
        return {
            statePath: contracts_1.STRUCTURAL_PRESSURE_STATE_PATH,
            statePresent: false,
            sourceMilestoneReconciliationPath: null,
            sourceMilestoneOverviewPath: null,
            syncedAt: null,
            thresholds: options.thresholds,
            entries: [],
            driftFindings: [],
            scan
        };
    }
    const state = (0, state_1.parseStructuralPressureState)(node_fs_1.default.readFileSync(stateAbsolutePath, "utf8"), contracts_1.STRUCTURAL_PRESSURE_STATE_PATH);
    const scan = (0, scan_1.scanStructuralPressureSources)(repositoryRoot, {
        scanPath: options.scanPath,
        thresholds: options.thresholds,
        entries: state.entries,
        exclusions
    });
    return {
        statePath: contracts_1.STRUCTURAL_PRESSURE_STATE_PATH,
        statePresent: true,
        sourceMilestoneReconciliationPath: state.source.milestone_reconciliation,
        sourceMilestoneOverviewPath: state.source.milestone_overview,
        syncedAt: state.synced_at,
        thresholds: options.thresholds,
        entries: state.entries,
        driftFindings: collectStructuralPressureDriftFindings(repositoryRoot, state),
        scan
    };
}
function collectStructuralPressureDriftFindings(repositoryRoot, state) {
    const findings = [];
    const reviewedReconciliations = new Map();
    const collectForReconciliation = (reconciliationPath, milestoneOverviewPath, entryPath) => {
        const reconciliationAbsolutePath = (0, paths_1.resolveRepositoryRelativePath)(repositoryRoot, reconciliationPath);
        if (!node_fs_1.default.existsSync(reconciliationAbsolutePath)) {
            findings.push({
                derivedStatePath: contracts_1.STRUCTURAL_PRESSURE_STATE_PATH,
                canonicalMarkdownPath: reconciliationPath,
                entryPath,
                reason: "Derived JSON points at a reconciliation artifact that is missing; canonical Markdown evidence is authoritative.",
                severity: "warning"
            });
            return;
        }
        if (!reviewedReconciliations.has(reconciliationPath)) {
            const review = (0, implementation_summary_reconciliation_1.reviewReconciliation)(repositoryRoot, {
                milestoneOverviewPath
            });
            reviewedReconciliations.set(reconciliationPath, [
                ...review.evidenceGaps,
                ...review.placeholderResidue,
                ...review.alignmentIssues
            ]);
        }
        const reviewIssues = reviewedReconciliations.get(reconciliationPath) ?? [];
        for (const reviewIssue of reviewIssues) {
            findings.push({
                derivedStatePath: contracts_1.STRUCTURAL_PRESSURE_STATE_PATH,
                canonicalMarkdownPath: reconciliationPath,
                entryPath,
                reason: `Derived JSON points at reconciliation evidence that is not completed: ${reviewIssue}`,
                severity: "warning"
            });
        }
    };
    collectForReconciliation(state.source.milestone_reconciliation, state.source.milestone_overview, null);
    for (const entry of state.entries) {
        collectForReconciliation(entry.source_milestone_reconciliation, entry.source_milestone_overview, entry.path);
    }
    return findings;
}
