import type { PickerCapability, PickerExcluded, PickerNode } from '../../types/filePicker' export function normalizePickerFormat(format: string): string { return format.trim().replace(/^\./, '').toLowerCase() } export function pickerExclusion( node: PickerNode, capability?: PickerCapability, skipBlanks = false, ): PickerExcluded | null { const base = { path: node.path, name: node.name } if (node.unavailableReason) return { ...base, reason: 'unreadable' } if (skipBlanks && capability?.isBlank?.(node)) return { ...base, reason: 'blank' } if (node.kind === 'folder') return null const formats = capability?.formats.map(normalizePickerFormat) ?? [] if (formats.length && !formats.some((format) => node.name.toLowerCase().endsWith(`.${format}`))) { return { ...base, reason: 'unsupported' } } if (capability?.maxBytes != null && node.bytes != null && node.bytes > capability.maxBytes) { return { ...base, reason: 'tooLarge' } } return null } export function pickerReason(excluded: PickerExcluded | null, node?: PickerNode): string { if (!excluded) return '' return { unsupported: 'This format is not supported by this source.', tooLarge: 'This file exceeds the size limit for this source.', blank: 'Skipped by the blank-file rule.', unreadable: node?.unavailableReason || 'This item cannot be read.', deselected: 'Removed from the selection.', forbidden: 'This item is not permitted by the system filter.', }[excluded.reason] } export function pickerFormat(name: string): string { const compound = /\.(mzml|mzxml)\.gz$/i.exec(name) return compound ? `${compound[1]}.gz` : name.includes('.') ? name.split('.').pop()! : '' }