import MemoryBudgetError from "../errors/MemoryBudgetError.js";
import { requireMemoryBudgetUnit } from "./MemoryBudgetUnit.js";
/** Immutable report of current memory-pool accounting. */
export default class WarpMemoryPoolSnapshot {
    name;
    limit;
    unit;
    leased;
    peak;
    rejected;
    constructor(fields) {
        const validFields = requireSnapshotFields(fields);
        this.name = requireNonEmptyString(validFields.name, 'name');
        this.limit = requirePositiveInteger(validFields.limit, 'limit');
        this.unit = requireMemoryBudgetUnit(validFields.unit);
        this.leased = requireNonNegativeInteger(validFields.leased, 'leased');
        this.peak = requireNonNegativeInteger(validFields.peak, 'peak');
        this.rejected = requireNonNegativeInteger(validFields.rejected, 'rejected');
        requireSnapshotInvariant(this.leased, this.peak);
        Object.freeze(this);
    }
}
function requireSnapshotFields(fields) {
    if (fields !== null && typeof fields === 'object') {
        return fields;
    }
    return throwSnapshotError('fields');
}
function requireNonEmptyString(value, field) {
    if (typeof value === 'string' && value.trim().length > 0) {
        return value;
    }
    return throwSnapshotError(field);
}
function requirePositiveInteger(value, field) {
    if (Number.isInteger(value) && value > 0) {
        return value;
    }
    return throwSnapshotError(field);
}
function requireNonNegativeInteger(value, field) {
    if (Number.isInteger(value) && value >= 0) {
        return value;
    }
    return throwSnapshotError(field);
}
function requireSnapshotInvariant(leased, peak) {
    if (peak >= leased) {
        return;
    }
    throwSnapshotError('peak');
}
function throwSnapshotError(field) {
    throw new MemoryBudgetError('WarpMemoryPoolSnapshot requires valid accounting fields', {
        code: 'E_MEMORY_BUDGET_INVALID',
        context: { field },
    });
}
