/**
 * Shared trailer validation helpers for WARP message codecs.
 *
 * Extracted from AnchorMessageCodec, AuditMessageCodec,
 * CheckpointMessageCodec, and PatchMessageCodec.
 */
import { TRAILER_KEYS } from "./MessageCodecInternal.js";
import MessageCodecError from "../../errors/MessageCodecError.js";
const KEYS = TRAILER_KEYS;
/** Asserts a required trailer field is present and returns its value. */
export function requireTrailer(trailers, key, kind) {
    const trailerName = String(KEYS[key]);
    const value = trailers[trailerName];
    if (typeof value !== 'string' || value.length === 0) {
        throw new MessageCodecError(`Invalid ${kind} message: missing required trailer ${trailerName}`, { code: 'E_MISSING_TRAILER' });
    }
    return value;
}
/** Parses a trailer value as a positive integer. */
export function parsePositiveIntTrailer(trailers, key, kind) {
    const str = requireTrailer(trailers, key, kind);
    const trailerName = String(KEYS[key]);
    if (!/^\d+$/.test(str)) {
        throw new MessageCodecError(`Invalid ${kind} message: ${trailerName} must be a positive integer, got '${str}'`, { code: 'E_INVALID_TRAILER' });
    }
    const num = Number(str);
    if (!Number.isInteger(num) || num < 1) {
        throw new MessageCodecError(`Invalid ${kind} message: ${trailerName} must be a positive integer, got '${str}'`, { code: 'E_INVALID_TRAILER' });
    }
    return num;
}
/** Validates the eg-kind discriminator trailer matches the expected kind. */
export function validateKindDiscriminator(trailers, expected) {
    const kind = trailers[String(KEYS['kind'])];
    if (kind !== expected) {
        const kindLabel = kind ?? 'missing';
        throw new MessageCodecError(`Invalid ${expected} message: eg-kind must be '${expected}', got '${kindLabel}'`, { code: 'E_WRONG_KIND' });
    }
}
