/**
 * Shared byte-size formatter.
 *
 * Single source of truth for "1536 -> 1.5 KB" style formatting, previously
 * duplicated as formatBytes in deletion/delete_manager.js and formatFileSize
 * in pdf_upload_handler.js. Loaded globally from base.html so both consumers
 * (and any future ones) can call window.formatBytes.
 */
(function() {
    'use strict';

    /**
     * Format a byte count as a human-readable size.
     * @param {number} bytes - Non-negative byte count.
     * @returns {string} e.g. "0 Bytes", "1.5 KB", "1 MB".
     */
    function formatBytes(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / k ** i).toFixed(2)) + ' ' + sizes[i];
    }

    window.formatBytes = formatBytes;
})();
