import VersionVector from "../crdt/VersionVector.js";
import WarpError from "../errors/WarpError.js";
import Patch from "../types/Patch.js";
/** Substrate replay facts carried by a git-warp patch at one tick. */
export default class GitWarpTickPatchReplayCore {
    patchSha;
    writer;
    lamport;
    operationCount;
    contextWriterCount;
    readCount;
    writeCount;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        const patch = requirePatch(checkedFields.patch);
        this.patchSha = requireNonEmptyString(checkedFields.patchSha, 'patchSha');
        this.writer = requireNonEmptyString(patch.writer, 'patch.writer');
        this.lamport = requireNonNegativeInteger(patch.lamport, 'patch.lamport');
        this.operationCount = patch.ops.length;
        this.contextWriterCount = VersionVector.from(patch.context).size;
        this.readCount = patch.reads?.length ?? 0;
        this.writeCount = patch.writes?.length ?? 0;
        Object.freeze(this);
    }
}
/** Validates the replay-core constructor envelope. */
function requireFields(value) {
    if (value === null || value === undefined) {
        throw new WarpError('GitWarpTickPatchReplayCore 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 required non-empty string. */
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;
}
/** Validates a non-negative integer. */
function requireNonNegativeInteger(value, name) {
    if (!Number.isInteger(value) || value < 0) {
        throw new WarpError(`${name} must be a non-negative integer`, 'E_VALIDATION');
    }
    return value;
}
