import GitWarpTickReceiptShell from "./GitWarpTickReceiptShell.js";
import GitWarpTickReceiptWitnessCore from "./GitWarpTickReceiptWitnessCore.js";
import WarpError from "../errors/WarpError.js";
import { TickReceipt } from "../types/TickReceipt.js";
export const GIT_WARP_RECEIPT_ENVELOPE_BOUNDARY_VERSION = 'git-warp.receipt-envelope-boundary/v1';
export const GIT_WARP_RECEIPT_ENVELOPE_FACT_KIND = 'git-warp.tick-receipt';
/** Stable substrate-owned receipt anchors for external envelope consumers. */
export default class GitWarpReceiptEnvelopeBoundary {
    boundaryVersion = GIT_WARP_RECEIPT_ENVELOPE_BOUNDARY_VERSION;
    substrateFactKind = GIT_WARP_RECEIPT_ENVELOPE_FACT_KIND;
    witnessCore;
    receiptShell;
    constructor(fields) {
        const receipt = requireReceipt(requireFields(fields).receipt);
        this.witnessCore = new GitWarpTickReceiptWitnessCore({ receipt });
        this.receiptShell = new GitWarpTickReceiptShell({ receipt });
        Object.freeze(this);
    }
    /** Returns the minimal public anchor without raw ops or debug reason text. */
    stableAnchor() {
        return Object.freeze({
            boundaryVersion: this.boundaryVersion,
            substrateFactKind: this.substrateFactKind,
            patchSha: this.witnessCore.patchSha,
            writer: this.witnessCore.writer,
            lamport: this.witnessCore.lamport,
            outcomeCount: this.witnessCore.outcomeCount,
            appliedCount: this.witnessCore.appliedCount,
            supersededCount: this.witnessCore.supersededCount,
            redundantCount: this.witnessCore.redundantCount,
            hasExplanatoryReasons: this.receiptShell.hasExplanatoryReasons(),
        });
    }
}
/** Validates the boundary constructor envelope. */
function requireFields(value) {
    if (value === null || value === undefined) {
        throw new WarpError('GitWarpReceiptEnvelopeBoundary fields must be provided', 'E_VALIDATION');
    }
    return value;
}
/** Validates a TickReceipt carrier. */
function requireReceipt(value) {
    if (!(value instanceof TickReceipt)) {
        throw new WarpError('receipt must be a TickReceipt', 'E_VALIDATION');
    }
    return value;
}
