"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTraceLinkDispositionApplyEvidence = createTraceLinkDispositionApplyEvidence;
exports.attachTraceLinkDispositionApplyEvidence = attachTraceLinkDispositionApplyEvidence;
exports.isTraceLinkDispositionRewriteEligible = isTraceLinkDispositionRewriteEligible;
exports.createTraceLinkDispositionRevision = createTraceLinkDispositionRevision;
const model_1 = require("./model");
function createTraceLinkDispositionApplyEvidence(input) {
    return {
        lastAppliedAt: requireNonEmptyString(input.lastAppliedAt, "lastAppliedAt"),
        dispositionRevision: requireNonEmptyString(input.dispositionRevision, "dispositionRevision"),
        changedPaths: normalizeEvidencePaths(input.changedPaths ?? []),
        skippedPaths: normalizeEvidencePaths(input.skippedPaths ?? []),
        conflictPaths: normalizeEvidencePaths(input.conflictPaths ?? []),
        unresolvedPaths: normalizeEvidencePaths(input.unresolvedPaths ?? [])
    };
}
function attachTraceLinkDispositionApplyEvidence(disposition, evidence) {
    return {
        ...disposition,
        applyEvidence: isApplyEvidence(evidence)
            ? createTraceLinkDispositionApplyEvidence(evidence)
            : createTraceLinkDispositionApplyEvidence(evidence)
    };
}
function isTraceLinkDispositionRewriteEligible(disposition) {
    return disposition.status === "migrated";
}
function createTraceLinkDispositionRevision(disposition) {
    const identity = (0, model_1.createTraceLinkDispositionId)(disposition.target.normalizedPath);
    return `${identity}@${disposition.updatedAt}`;
}
function normalizeEvidencePaths(paths) {
    return paths.map((path) => (0, model_1.normalizeTraceLinkDispositionTarget)(path).normalizedPath);
}
function requireNonEmptyString(value, fieldName) {
    if (value.trim() === "") {
        throw new RangeError(`Trace-link disposition apply evidence '${fieldName}' must be a non-empty string.`);
    }
    return value;
}
function isApplyEvidence(value) {
    return Array.isArray(value.changedPaths) &&
        Array.isArray(value.skippedPaths) &&
        Array.isArray(value.conflictPaths) &&
        Array.isArray(value.unresolvedPaths);
}
