import ContinuumArtifactDescriptor from "./ContinuumArtifactDescriptor.js";
import ContinuumEvidencePosture from "./ContinuumEvidencePosture.js";
import WarpError from "../errors/WarpError.js";
/** Couples a Continuum-family descriptor to an explicit evidence posture. */
export default class ContinuumEvidenceClaim {
    descriptor;
    posture;
    nativeWitnessProof;
    /** Builds an immutable evidence claim after validating its descriptor and posture. */
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.descriptor = requireDescriptor(checkedFields.descriptor);
        this.posture = requirePosture(checkedFields.posture);
        this.nativeWitnessProof = optionalNonEmptyString(checkedFields.nativeWitnessProof, 'nativeWitnessProof');
        this.assertNativeProofMatchesPosture();
        Object.freeze(this);
    }
    /** Returns true when this is git-warp evidence translated into Continuum shape. */
    isTranslatedGitWarpEvidence() {
        return this.posture.isTranslatedGitWarpEvidence();
    }
    /** Returns true only when explicit native Continuum witness proof exists. */
    isNativeContinuumEvidence() {
        return this.posture.isNativeContinuumEvidence();
    }
    /** Returns this claim only when translated git-warp evidence is explicit. */
    requireTranslatedGitWarpEvidence() {
        if (!this.isTranslatedGitWarpEvidence()) {
            throw new WarpError(`Continuum evidence for ${this.descriptor.familyId.toString()} must be translated git-warp evidence`, 'E_VALIDATION');
        }
        return this;
    }
    /** Enforces that native proof exists only for native Continuum evidence. */
    assertNativeProofMatchesPosture() {
        if (this.posture.requiresNativeWitnessProof() && this.nativeWitnessProof === undefined) {
            throw new WarpError('available native Continuum evidence requires nativeWitnessProof', 'E_VALIDATION');
        }
        if (!this.posture.isNativeContinuumEvidence() && this.nativeWitnessProof !== undefined) {
            throw new WarpError('nativeWitnessProof requires native Continuum evidence posture', 'E_VALIDATION');
        }
    }
}
/** Validates the claim constructor envelope. */
function requireFields(value) {
    if (value === null || value === undefined) {
        throw new WarpError('ContinuumEvidenceClaim fields must be provided', 'E_VALIDATION');
    }
    return value;
}
/** Validates a descriptor carrier. */
function requireDescriptor(value) {
    if (!(value instanceof ContinuumArtifactDescriptor)) {
        throw new WarpError('descriptor must be a ContinuumArtifactDescriptor', 'E_VALIDATION');
    }
    return value;
}
/** Validates an evidence posture carrier. */
function requirePosture(value) {
    if (!(value instanceof ContinuumEvidencePosture)) {
        throw new WarpError('posture must be a ContinuumEvidencePosture', 'E_VALIDATION');
    }
    return value;
}
/** Validates an optional non-empty string. */
function optionalNonEmptyString(value, name) {
    if (value === undefined) {
        return undefined;
    }
    if (typeof value !== 'string' || value.trim().length === 0) {
        throw new WarpError(`${name} must be a non-empty string`, 'E_VALIDATION');
    }
    return value;
}
