/**
 * Filesystem operation limits validation.
 *
 * Centralizes limit checks for file reads, writes, list entries,
 * tree depth, and search results. Handlers can call these utilities
 * to enforce limits and get consistent error responses.
 */
import { MAX_FILE_SIZE, MAX_WRITE_SIZE, MAX_FS_ENTRIES, MAX_TREE_DEPTH, MAX_SEARCH_RESULTS, ErrorCode, } from "@pocketcoder/shared/constants";
import { AppError } from "../../errors/index.js";
export { MAX_FILE_SIZE, MAX_WRITE_SIZE, MAX_FS_ENTRIES, MAX_TREE_DEPTH, MAX_SEARCH_RESULTS };
/**
 * Validate that a file size does not exceed the read limit (10 MB).
 * Throws AppError with FILE_TOO_LARGE if exceeded.
 */
export function validateReadSize(size) {
    if (size > MAX_FILE_SIZE) {
        throw new AppError(ErrorCode.FILE_TOO_LARGE, `File exceeds maximum size of ${formatBytes(MAX_FILE_SIZE)}`, { size, maxSize: MAX_FILE_SIZE });
    }
}
/**
 * Validate that content size does not exceed the write limit (1 MB).
 * Throws AppError with FILE_TOO_LARGE if exceeded.
 */
export function validateWriteSize(size) {
    if (size > MAX_WRITE_SIZE) {
        throw new AppError(ErrorCode.FILE_TOO_LARGE, `Content exceeds maximum size of ${formatBytes(MAX_WRITE_SIZE)}`, { size, maxSize: MAX_WRITE_SIZE });
    }
}
/**
 * Truncate a list of entries to the MAX_FS_ENTRIES limit.
 * Returns the truncated array and a flag indicating whether truncation occurred.
 */
export function truncateListEntries(entries) {
    if (entries.length <= MAX_FS_ENTRIES) {
        return { entries, truncated: false };
    }
    return { entries: entries.slice(0, MAX_FS_ENTRIES), truncated: true };
}
/**
 * Cap a requested tree depth at the MAX_TREE_DEPTH limit.
 * Returns the capped value (always <= MAX_TREE_DEPTH).
 */
export function capTreeDepth(requestedDepth) {
    return Math.min(Math.max(requestedDepth, 0), MAX_TREE_DEPTH);
}
/**
 * Truncate search results to the MAX_SEARCH_RESULTS limit.
 * Returns the truncated array and a flag indicating whether truncation occurred.
 */
export function truncateSearchResults(results) {
    if (results.length <= MAX_SEARCH_RESULTS) {
        return { results, truncated: false };
    }
    return { results: results.slice(0, MAX_SEARCH_RESULTS), truncated: true };
}
/**
 * Format byte count as human-readable string.
 */
function formatBytes(bytes) {
    if (bytes >= 1024 * 1024) {
        return `${bytes / (1024 * 1024)}MB`;
    }
    if (bytes >= 1024) {
        return `${bytes / 1024}KB`;
    }
    return `${bytes}B`;
}
//# sourceMappingURL=limits.js.map