import MemoryBudgetError from "../errors/MemoryBudgetError.js";
import { MEMORY_BUDGET_UNIT_BYTE, MEMORY_BUDGET_UNIT_ENTRY, MEMORY_BUDGET_UNIT_FACT, MEMORY_BUDGET_UNIT_PATCH, MEMORY_BUDGET_UNIT_RESULT, requireMemoryBudgetUnit, } from "./MemoryBudgetUnit.js";
/** Immutable limit for git-warp-owned decoded buffers, caches, and windows. */
export default class MemoryBudget {
    limit;
    unit;
    constructor(fields) {
        const validFields = requireMemoryBudgetFields(fields);
        this.limit = requirePositiveInteger(validFields.limit, 'limit');
        this.unit = requireMemoryBudgetUnit(validFields.unit);
        Object.freeze(this);
    }
    static bytes(limit) {
        return new MemoryBudget({ limit, unit: MEMORY_BUDGET_UNIT_BYTE });
    }
    static entries(limit) {
        return new MemoryBudget({ limit, unit: MEMORY_BUDGET_UNIT_ENTRY });
    }
    static patches(limit) {
        return new MemoryBudget({ limit, unit: MEMORY_BUDGET_UNIT_PATCH });
    }
    static facts(limit) {
        return new MemoryBudget({ limit, unit: MEMORY_BUDGET_UNIT_FACT });
    }
    static results(limit) {
        return new MemoryBudget({ limit, unit: MEMORY_BUDGET_UNIT_RESULT });
    }
}
function requireMemoryBudgetFields(fields) {
    if (fields !== null && typeof fields === 'object') {
        return fields;
    }
    throw new MemoryBudgetError('MemoryBudget requires object fields', {
        code: 'E_MEMORY_BUDGET_INVALID',
        context: { field: 'fields' },
    });
}
function requirePositiveInteger(value, field) {
    if (Number.isInteger(value) && value > 0) {
        return value;
    }
    throw new MemoryBudgetError('Memory budget limit must be a positive integer', {
        code: 'E_MEMORY_BUDGET_INVALID',
        context: { field, value },
    });
}
