import MemoryBudgetError from "../../errors/MemoryBudgetError.js";
/** Immutable bounded page of query node snapshots. */
export default class BoundedQueryNodePage {
    nodes;
    cursor;
    constructor(fields) {
        const validFields = requirePageFields(fields);
        this.nodes = freezeNodes(validFields.nodes);
        this.cursor = normalizeCursor(validFields.cursor);
        Object.freeze(this);
    }
}
function requirePageFields(fields) {
    if (fields !== null && typeof fields === 'object') {
        return fields;
    }
    throw new MemoryBudgetError('Bounded query node page requires object fields', {
        code: 'E_BOUNDED_QUERY_PAGE_INVALID',
        context: { field: 'fields' },
    });
}
function freezeNodes(values) {
    if (!Array.isArray(values)) {
        throw new MemoryBudgetError('Bounded query node page requires an array of nodes', {
            code: 'E_BOUNDED_QUERY_PAGE_INVALID',
            context: { field: 'nodes' },
        });
    }
    const nodes = [];
    for (const value of values) {
        nodes.push(value);
    }
    return Object.freeze(nodes);
}
function normalizeCursor(value) {
    if (value === null) {
        return null;
    }
    if (typeof value === 'string' && value.length > 0) {
        return value;
    }
    throw new MemoryBudgetError('Bounded query node page cursor must be non-empty or null', {
        code: 'E_BOUNDED_QUERY_PAGE_INVALID',
        context: { field: 'cursor' },
    });
}
