import { l10n } from './i18n.js';
/**
 * Validate a File against an `accept` attribute string, matching the HTML
 * `<input accept>` attribute format. Client-side filter only — server-side
 * validation is still required.
 *
 * Supported forms (comma-separated, any combination):
 * - `image/*` — MIME wildcard (any image)
 * - `application/pdf` — exact MIME type
 * - `.pdf`, `.docx` — file extension (case-insensitive)
 *
 * Examples:
 * - `matchesAccept(file, "image/*")` — any image
 * - `matchesAccept(file, "image/*,application/pdf")` — images and PDFs
 * - `matchesAccept(file, ".jpg,.jpeg,.png,.heic")` — specific extensions
 * - `matchesAccept(file, "")` — allow all (returns true)
 *
 * Note: users can bypass the file picker's filter (most browsers offer an
 * "All Files" option), so this function is a UX filter, not a security
 * boundary.
 */
export function matchesAccept(file, accept) {
    if (!accept || !accept.trim())
        return true;
    const types = accept
        .split(',')
        .map(t => t.trim().toLowerCase())
        .filter(Boolean);
    const fileMime = file.type.toLowerCase();
    const fileName = file.name.toLowerCase();
    return types.some(type => {
        if (type.startsWith('.')) {
            return fileName.endsWith(type);
        }
        if (type.endsWith('/*')) {
            const category = type.slice(0, type.indexOf('/'));
            return fileMime.startsWith(category + '/');
        }
        return fileMime === type;
    });
}
/**
 * Validate a File's size against a maximum byte limit.
 * Returns false if file exceeds `maxSize` bytes.
 * Returns true if `maxSize` is 0 (unlimited).
 */
export function validateFileSize(file, maxSize) {
    if (maxSize <= 0)
        return true;
    return file.size <= maxSize;
}
/**
 * Resolve a MIME type to a Lucide icon symbol name.
 *
 * Mappings:
 * - `image/*` → `'image'`
 * - `video/*` → `'film'`
 * - `audio/*` → `'music'`
 * - document types → `'file'`
 * - everything else → `'upload-cloud'`
 */
export function resolveFileSymbol(mimeType) {
    if (!mimeType)
        return 'upload-cloud';
    const mime = mimeType.toLowerCase();
    if (mime.startsWith('image/'))
        return 'image';
    if (mime.startsWith('video/'))
        return 'film';
    if (mime.startsWith('audio/'))
        return 'music';
    if (mime.startsWith('text/') ||
        mime === 'application/pdf' ||
        mime === 'application/msword' ||
        mime.startsWith('application/vnd.openxmlformats')) {
        return 'file';
    }
    return 'upload-cloud';
}
/**
 * Format a byte count to a human-readable file size string.
 *
 * Output format: `"200 KB"`, `"4.2 MB"`, `"16 MB"`, `"1.3 GB"`
 * Uses `l10n()` for unit labels.
 *
 * Thresholds:
 * - 0 bytes → `"< 1 KB"`
 * - < 1 KB → `"< 1 KB"` (sub-KB files render the same)
 * - < 1 MB → whole number KB
 * - < 1 GB → one decimal MB
 * - >= 1 GB → one decimal GB
 */
export function formatFileSize(bytes) {
    const KB = 1024;
    const MB = KB * 1024;
    const GB = MB * 1024;
    if (bytes < KB) {
        return l10n('upload', 'sizeSubKB', '< 1 KB');
    }
    if (bytes < MB) {
        const kb = Math.round(bytes / KB);
        return `${kb} ${l10n('upload', 'unitKB', 'KB')}`;
    }
    if (bytes < GB) {
        const mb = bytes / MB;
        const formatted = mb >= 10 ? Math.round(mb).toString() : mb.toFixed(1);
        return `${formatted} ${l10n('upload', 'unitMB', 'MB')}`;
    }
    const gb = bytes / GB;
    const formatted = gb >= 10 ? Math.round(gb).toString() : gb.toFixed(1);
    return `${formatted} ${l10n('upload', 'unitGB', 'GB')}`;
}
//# sourceMappingURL=upload.js.map