import GitWarpTickPatchReplayCore from "./GitWarpTickPatchReplayCore.js";
import GitWarpTickReceiptShell from "./GitWarpTickReceiptShell.js";
import GitWarpTickReceiptWitnessCore from "./GitWarpTickReceiptWitnessCore.js";
import WarpError from "../errors/WarpError.js";
import Patch from "../types/Patch.js";
import { TickReceipt } from "../types/TickReceipt.js";
/** Validated witness ladder for one git-warp patch tick and receipt. */
export default class GitWarpTickWitnessLadder {
    replayCore;
    witnessCore;
    receiptShell;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        const patch = requirePatch(checkedFields.patch);
        const receipt = requireReceipt(checkedFields.receipt);
        this.replayCore = new GitWarpTickPatchReplayCore({
            patch,
            patchSha: checkedFields.patchSha,
        });
        this.witnessCore = new GitWarpTickReceiptWitnessCore({ receipt });
        this.receiptShell = new GitWarpTickReceiptShell({ receipt });
        assertAlignedTick(this.replayCore, this.witnessCore);
        Object.freeze(this);
    }
}
/** Validates the ladder constructor envelope. */
function requireFields(value) {
    if (value === null || value === undefined) {
        throw new WarpError('GitWarpTickWitnessLadder fields must be provided', 'E_VALIDATION');
    }
    return value;
}
/** Validates a Patch carrier. */
function requirePatch(value) {
    if (!(value instanceof Patch)) {
        throw new WarpError('patch must be a Patch', '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;
}
/** Requires replay and receipt facts to describe the same tick. */
function assertAlignedTick(replayCore, witnessCore) {
    if (replayCore.patchSha !== witnessCore.patchSha) {
        throw new WarpError('tick witness ladder patch SHA must match receipt patch SHA', 'E_VALIDATION');
    }
    if (replayCore.writer !== witnessCore.writer) {
        throw new WarpError('tick witness ladder writer must match receipt writer', 'E_VALIDATION');
    }
    if (replayCore.lamport !== witnessCore.lamport) {
        throw new WarpError('tick witness ladder Lamport must match receipt Lamport', 'E_VALIDATION');
    }
}
