{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-file-uploader",
  "type": "registry:lib",
  "title": "core/file-uploader",
  "description": "Framework-free behaviour shared by the ui-* components (file-uploader); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-big-number",
    "@kanok-ui/core-i18n"
  ],
  "files": [
    {
      "path": "src/core/file-uploader.ts",
      "type": "registry:lib",
      "target": "components/ui/core/file-uploader.ts",
      "content": "/**\n * Carbon `FileUploader` rules, framework-free: which picked files `accept` and the size limit let\n * through (Carbon `validateFiles`), the trigger's text after a pick, and where focus goes when a\n * listed file is removed. Beyond Carbon: the file count / total size limits, a file's size and its\n * upload percentage as text.\n */\nimport { supportedLocale } from './big-number.ts';\nimport type { FileUploaderMessages } from './i18n.ts';\n\nexport type FileStatus = 'edit' | 'complete' | 'uploading';\n\n/** Why a file was refused, or null when it is accepted. `too-many` / `total-too-large`: over `maxFiles` / `maxTotalSize`. */\nexport type FileRejection = 'too-large' | 'invalid-type' | 'too-many' | 'total-too-large' | null;\n\n/** `accept` (\".pdf, image/*, image/png\") as lower-case entries. */\nexport const acceptEntries = (accept: string) =>\n    accept\n        .split(',')\n        .map((t) => t.trim().toLowerCase())\n        .filter(Boolean);\n\n/** One `accept` entry, matched as the file dialog does: `.pdf` by extension, `image/*` by MIME group, `image/png` exactly. */\nexport function acceptMatches(entry: string, type: string, ext: string | undefined): boolean {\n    if (entry.startsWith('.')) return entry === ext;\n    if (entry.endsWith('/*')) return type.startsWith(entry.slice(0, -1));\n    return entry === type;\n}\n\n/** Carbon `validateFiles`: the size limit first, then the extension or MIME type against `accept` (empty accepts all). */\nexport function checkFile(file: { name: string; type: string; size: number }, rules: { accept?: string; maxFileSize?: number }): FileRejection {\n    if (rules.maxFileSize && rules.maxFileSize > 0 && file.size > rules.maxFileSize) return 'too-large';\n    const accepted = acceptEntries(rules.accept ?? '');\n    if (!accepted.length) return null;\n    const ext = /\\.[0-9a-z]+$/i.exec(file.name)?.[0].toLowerCase();\n    const type = file.type.toLowerCase();\n    return accepted.some((entry) => acceptMatches(entry, type, ext)) ? null : 'invalid-type';\n}\n\n/** The uploader's limits: `accept` and `maxFileSize` per file (Carbon), `maxFiles` and `maxTotalSize` (bytes) over the list; 0 = no limit. */\nexport interface FileRules {\n    accept?: string;\n    maxFileSize?: number;\n    maxFiles?: number;\n    maxTotalSize?: number;\n}\n\n/**\n * Every picked file's rejection, in order: each file's own check (`checkFile`) first, then whether it\n * still fits the count and the total size next to the files already `kept` (the accepted ones in the\n * list) and the picked files accepted before it. Refused files don't count towards the limits.\n */\nexport function checkFiles(files: readonly { name: string; type: string; size: number }[], rules: FileRules, kept: readonly { size: number }[] = []): FileRejection[] {\n    let count = kept.length;\n    let total = kept.reduce((sum, f) => sum + f.size, 0);\n    return files.map((file) => {\n        const rejection = checkFile(file, rules);\n        if (rejection) return rejection;\n        if (rules.maxFiles && rules.maxFiles > 0 && count >= rules.maxFiles) return 'too-many';\n        if (rules.maxTotalSize && rules.maxTotalSize > 0 && total + file.size > rules.maxTotalSize) return 'total-too-large';\n        count++;\n        total += file.size;\n        return null;\n    });\n}\n\n/** The uploader's own texts for refused files (each falls back to the translation; `invalidBodyText` is every refused file's body). */\nexport interface RejectionTexts {\n    invalidTypeText?: string;\n    tooLargeText?: string;\n    tooManyText?: string;\n    totalTooLargeText?: string;\n    invalidBodyText?: string;\n}\n\n/** A refused file's error subject and body: the uploader's text, else the translation (the limits say what they are). */\nexport function rejectionMessage(rejection: Exclude<FileRejection, null>, t: FileUploaderMessages, texts: RejectionTexts, rules: FileRules & { locale?: string }) {\n    const subjects = { 'too-large': texts.tooLargeText || t.tooLarge, 'invalid-type': texts.invalidTypeText || t.invalidType, 'too-many': texts.tooManyText || t.tooMany, 'total-too-large': texts.totalTooLargeText || t.totalTooLarge };\n    let body = t.invalidBody;\n    if (rejection === 'too-many') body = t.tooManyBody(rules.maxFiles ?? 0);\n    else if (rejection === 'total-too-large') body = t.totalTooLargeBody(formatFileSize(rules.maxTotalSize ?? 0, rules.locale));\n    return { errorSubject: subjects[rejection], errorBody: texts.invalidBodyText || body };\n}\n\nconst SIZE_UNITS = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'] as const;\n\n/**\n * A file size for people, in the page language: \"1.2 MB\", \"512 byte\" (Thai \"512 ไบต์\") — `Intl`\n * short units, decimal (1 kB = 1000 bytes, as the unit names say), one decimal at most.\n */\nexport function formatFileSize(bytes: number, locale?: string): string {\n    let value = Math.max(0, Number.isFinite(bytes) ? bytes : 0);\n    let unit = 0;\n    // step up while the rounded value would read 1000 or more (\"1,000 kB\" is \"1 MB\")\n    while (unit < SIZE_UNITS.length - 1 && Math.round(value * 10) / 10 >= 1000) {\n        value /= 1000;\n        unit++;\n    }\n    return new Intl.NumberFormat(supportedLocale(locale), { style: 'unit', unit: SIZE_UNITS[unit], unitDisplay: 'short', maximumFractionDigits: 1 }).format(value);\n}\n\n/** An item's error subject: its own, else \"Upload failed\" on a retryable (failed) row; '' when valid or without one. */\nexport function itemErrorSubject(item: { invalid: boolean; retryable: boolean; errorSubject: string }, t: Pick<FileUploaderMessages, 'uploadFailed'>): string {\n    if (!item.invalid) return '';\n    if (item.errorSubject) return item.errorSubject;\n    return item.retryable ? t.uploadFailed : '';\n}\n\n/** An upload's `progress` as a whole percentage 0–100, or undefined when it isn't a number (no progress to show). */\nexport function uploadPercent(progress: number | null | undefined): number | undefined {\n    if (typeof progress !== 'number' || !Number.isFinite(progress)) return undefined;\n    return Math.round(Math.min(100, Math.max(0, progress)));\n}\n\n/** The percentage as text in the page language (\"42%\"). */\nexport const formatPercent = (percent: number, locale?: string) => new Intl.NumberFormat(supportedLocale(locale), { style: 'percent' }).format(percent / 100);\n\n/** The trigger's text after a pick (Carbon): the file's name, or \"N files\". */\nexport const pickedFilesLabel = (names: readonly string[], filesChosen: (n: number) => string) => (names.length > 1 ? filesChosen(names.length) : (names[0] ?? ''));\n\n/**\n * Where focus goes when the focused file at `index` is removed (it would fall to `<body>`): the\n * next removable file (`removable` lists the remaining ones), else the previous, else -1 for the\n * trigger.\n */\nexport function focusAfterRemoval(removable: readonly boolean[], index: number): number {\n    for (let i = index; i < removable.length; i++) if (removable[i]) return i;\n    for (let i = Math.min(index, removable.length) - 1; i >= 0; i--) if (removable[i]) return i;\n    return -1;\n}\n"
    }
  ]
}
