import ApertureOpeningProof from "./ApertureOpeningProof.js";
import ZKWormholeEdge from "./ZKWormholeEdge.js";
import WarpError from "../../errors/WarpError.js";
import WormholeError from "../../errors/WormholeError.js";
export async function verifyZKWormhole(edge, verifier) {
    return await requireVerifier(verifier).verifyTransition(requireEdge(edge));
}
export async function openAperture(edge, opening, verifier) {
    const checkedEdge = requireEdge(edge);
    const checkedOpening = requireOpening(opening);
    requireTickInRange(checkedEdge, checkedOpening.evaluatedTick);
    return await requireVerifier(verifier).verifyOpening(checkedEdge, checkedOpening);
}
function requireEdge(value) {
    if (!(value instanceof ZKWormholeEdge)) {
        throw new WarpError('edge must be a ZKWormholeEdge', 'E_VALIDATION');
    }
    return value;
}
function requireOpening(value) {
    if (!(value instanceof ApertureOpeningProof)) {
        throw new WarpError('opening must be an ApertureOpeningProof', 'E_VALIDATION');
    }
    return value;
}
function requireVerifier(value) {
    if (value === null
        || value === undefined
        || typeof value.verifyTransition !== 'function'
        || typeof value.verifyOpening !== 'function') {
        throw new WarpError('verifier must be a ZKWormholeProofVerifierPort', 'E_VALIDATION');
    }
    return value;
}
function requireTickInRange(edge, tick) {
    if (!edge.containsTick(tick)) {
        throw new WormholeError(`aperture tick ${String(tick)} is outside wormhole range`, {
            code: 'E_WORMHOLE_INVALID_RANGE',
            context: {
                tick,
                startTick: edge.startTick,
                endTick: edge.endTick,
            },
        });
    }
}
