import { TranslateFn } from '@/types'

export function getAcceptedTypesText(
	acceptedTypes = '*',
	t: TranslateFn
): string {
	if (acceptedTypes === '*') return t('types.all')

	const types = acceptedTypes.split(',').map(type => {
		if (type.startsWith('.')) return type
		if (type.endsWith('/*')) return type.replace('/*', ` ${t('files')}`)
		return type
	})

	return types.join(', ').replace(/\./g, '')
}

////////////

export function formatFileSize(bytes: number, t: TranslateFn): string {
	if (bytes === 0) return `0 ${t('bytes')}`
	const k = 1024
	const sizes = ['Bytes', 'KB', 'MB', 'GB']
	const i = Math.floor(Math.log(bytes) / Math.log(k))
	return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
