import ImmutableBytes from "../snapshot/ImmutableBytes.js";
import WarpError from "../../errors/WarpError.js";
/** Cold-tier ZK-Verkle wormhole edge for one compressed execution segment. */
export default class ZKWormholeEdge {
    fromSha;
    toSha;
    writerId;
    startTick;
    endTick;
    startStateRoot;
    endStateRoot;
    spaceTimeCommitment;
    transitionProof;
    patchCount;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.fromSha = requireNonEmptyString(checkedFields.fromSha, 'fromSha');
        this.toSha = requireNonEmptyString(checkedFields.toSha, 'toSha');
        this.writerId = requireNonEmptyString(checkedFields.writerId, 'writerId');
        this.startTick = requireNonNegativeInteger(checkedFields.startTick, 'startTick');
        this.endTick = requireEndTick(checkedFields.endTick, this.startTick);
        this.startStateRoot = requireNonEmptyString(checkedFields.startStateRoot, 'startStateRoot');
        this.endStateRoot = requireNonEmptyString(checkedFields.endStateRoot, 'endStateRoot');
        this.spaceTimeCommitment = requireNonEmptyString(checkedFields.spaceTimeCommitment, 'spaceTimeCommitment');
        this.transitionProof = requireNonEmptyBytes(checkedFields.transitionProof, 'transitionProof');
        this.patchCount = requirePositiveInteger(checkedFields.patchCount, 'patchCount');
        Object.freeze(this);
    }
    containsTick(tick) {
        return Number.isInteger(tick) && tick >= this.startTick && tick <= this.endTick;
    }
    transitionProofBytes() {
        return this.transitionProof.toUint8Array();
    }
}
function requireFields(value) {
    if (value === null || value === undefined) {
        throw new WarpError('ZKWormholeEdge fields must be provided', 'E_VALIDATION');
    }
    return value;
}
function requireEndTick(value, startTick) {
    const endTick = requireNonNegativeInteger(value, 'endTick');
    if (endTick < startTick) {
        throw new WarpError('endTick must be greater than or equal to startTick', 'E_VALIDATION');
    }
    return endTick;
}
function requireNonEmptyBytes(value, name) {
    if (!(value instanceof Uint8Array) || value.length === 0) {
        throw new WarpError(`${name} must be a non-empty Uint8Array`, 'E_VALIDATION');
    }
    return new ImmutableBytes(value);
}
function requireNonEmptyString(value, name) {
    if (typeof value !== 'string' || value.length === 0) {
        throw new WarpError(`${name} must be a non-empty string`, 'E_VALIDATION');
    }
    return value;
}
function requireNonNegativeInteger(value, name) {
    if (!Number.isInteger(value) || value < 0) {
        throw new WarpError(`${name} must be a non-negative integer`, 'E_VALIDATION');
    }
    return value;
}
function requirePositiveInteger(value, name) {
    if (!Number.isInteger(value) || value <= 0) {
        throw new WarpError(`${name} must be a positive integer`, 'E_VALIDATION');
    }
    return value;
}
