"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.previewTraceLinkDispositionUpdates = previewTraceLinkDispositionUpdates;
exports.renderTraceLinkDispositionPreviewText = renderTraceLinkDispositionPreviewText;
const discovery_1 = require("./discovery");
function previewTraceLinkDispositionUpdates(options) {
    const discovery = (0, discovery_1.discoverTraceLinkDispositionAffectedArtifacts)({
        repositoryRoot: options.repositoryRoot,
        dispositions: options.dispositions,
        artifactPath: options.artifactPath,
        planningRootPath: options.planningRootPath,
        specIndexPath: options.specIndexPath
    });
    const dispositions = options.dispositions.filter(isTraceLinkDisposition);
    const dispositionsById = new Map(dispositions.map((disposition) => [disposition.id, disposition]));
    const proposals = [];
    const lifecycleEvidence = [];
    const unresolved = [];
    for (const reference of discovery.references) {
        const disposition = dispositionsById.get(reference.dispositionId);
        if (disposition === undefined) {
            unresolved.push(createUnresolved(reference, "Matched reference does not have a valid selected disposition record."));
            continue;
        }
        if (disposition.status === "migrated") {
            proposals.push(createProposal(reference, disposition));
            continue;
        }
        lifecycleEvidence.push({
            dispositionId: disposition.id,
            dispositionStatus: disposition.status,
            sourceArtifactPath: reference.sourceArtifactPath,
            traceField: reference.traceField,
            relationship: reference.relationship,
            targetPath: reference.normalizedTargetPath,
            originalValue: reference.originalValue,
            message: `${disposition.status} dispositions are lifecycle evidence and do not produce rewrite proposals.`,
            noWriteRequired: true
        });
    }
    const skipped = [...discovery.skipped];
    return {
        readOnly: true,
        writeCapable: false,
        localOnly: true,
        dispositionStoragePath: options.dispositionStoragePath,
        selectedDispositionCount: dispositions.length,
        affectedArtifacts: discovery.affectedArtifacts,
        proposals,
        lifecycleEvidence,
        skipped,
        conflicts: [],
        unresolved,
        summary: {
            selectedDispositionCount: dispositions.length,
            affectedArtifactCount: discovery.summary.affectedArtifactCount,
            proposedUpdateCount: proposals.length,
            lifecycleEvidenceCount: lifecycleEvidence.length,
            skippedEvidenceCount: skipped.length,
            conflictCount: 0,
            unresolvedCount: unresolved.length
        }
    };
}
function renderTraceLinkDispositionPreviewText(result) {
    const lines = [
        "Trace-link disposition preview",
        `Read-only: ${String(result.readOnly)}`,
        `Storage: ${result.dispositionStoragePath}`,
        `Selected dispositions: ${result.summary.selectedDispositionCount}`,
        `Affected artifacts: ${result.summary.affectedArtifactCount}`,
        `Proposed updates: ${result.summary.proposedUpdateCount}`,
        ""
    ];
    if (result.proposals.length === 0) {
        lines.push("Proposed migrated-target updates: none.");
    }
    else {
        lines.push("Proposed migrated-target updates:");
        for (const proposal of result.proposals) {
            lines.push(`- ${proposal.sourceArtifactPath} ${proposal.traceField}: ${proposal.oldTargetPath} -> ${proposal.newTargetPath}`);
        }
    }
    if (result.lifecycleEvidence.length > 0) {
        lines.push("", "Lifecycle-only evidence:");
        for (const evidence of result.lifecycleEvidence) {
            lines.push(`- ${evidence.sourceArtifactPath} ${evidence.traceField}: ${evidence.dispositionStatus} ${evidence.targetPath}`);
        }
    }
    if (result.skipped.length > 0) {
        lines.push("", "Skipped evidence:");
        for (const evidence of result.skipped) {
            const path = evidence.sourceArtifactPath ?? evidence.dispositionTargetPath ?? evidence.dispositionId ?? "(unknown)";
            lines.push(`- ${evidence.reason}: ${path} - ${evidence.message}`);
        }
    }
    lines.push("", "No files were written.");
    return `${lines.join("\n")}\n`;
}
function createProposal(reference, disposition) {
    return {
        dispositionId: disposition.id,
        sourceArtifactPath: reference.sourceArtifactPath,
        artifactFamily: reference.artifactFamily,
        traceField: reference.traceField,
        relationship: reference.relationship,
        oldTargetPath: disposition.target.normalizedPath,
        newTargetPath: disposition.replacementTarget.normalizedPath,
        originalValue: reference.originalValue,
        status: "proposed",
        noWriteRequired: true
    };
}
function createUnresolved(reference, message) {
    return {
        reason: "disposition-malformed",
        dispositionId: reference.dispositionId,
        dispositionTargetPath: reference.dispositionTargetPath,
        sourceArtifactPath: reference.sourceArtifactPath,
        traceField: reference.traceField,
        message,
        noWriteRequired: true
    };
}
function isTraceLinkDisposition(value) {
    return typeof value === "object" &&
        value !== null &&
        "id" in value &&
        typeof value.id === "string" &&
        "status" in value &&
        (value.status === "retired" || value.status === "deleted" || value.status === "migrated");
}
