import QueryError from "../../errors/QueryError.js";
import { EventId } from "../../utils/EventId.js";
import { isPropValue } from "../../types/PropValue.js";
export function eventTransport(eventId) {
    return {
        lamport: eventId.lamport,
        writerId: eventId.writerId,
        patchSha: eventId.patchSha,
        opIndex: eventId.opIndex,
    };
}
export function eventSortKey(eventId) {
    return [
        String(eventId.lamport).padStart(12, '0'),
        eventId.writerId,
        eventId.patchSha,
        String(eventId.opIndex).padStart(8, '0'),
    ].join(':');
}
export function validateDirection(direction) {
    if (direction !== 'outgoing' && direction !== 'incoming') {
        throwFactError('direction', 'invalid-direction');
    }
    return direction;
}
export function validatePropValue(value) {
    if (!isPropValue(value)) {
        throwFactError('value', 'invalid-property-value');
    }
    return value;
}
export function validateEventId(eventId) {
    if (!(eventId instanceof EventId)) {
        throwFactError('eventId', 'invalid-event-id');
    }
    return eventId;
}
export function validateText(value, field) {
    if (typeof value !== 'string' || value.length === 0) {
        throwFactError(field, 'empty-string');
    }
    return value;
}
export function validatePositiveInteger(value, field) {
    if (!Number.isInteger(value) || value <= 0) {
        throwFactError(field, 'invalid-positive-integer');
    }
    return value;
}
function throwFactError(field, reason) {
    throw new QueryError('Checkpoint basis fact is invalid.', {
        code: 'E_CHECKPOINT_BASIS_FACT',
        context: { field, reason },
    });
}
