import { getFileIcon } from './fileIcons'

export const getPreviewType = (document) => {
  if (!document) { return null }
  const mime = (document.mimeType || '').toLowerCase()

  if (mime.startsWith('image/')) { return 'image' }
  if (mime === 'application/pdf') { return 'pdf' }
  if (mime.startsWith('audio/')) { return 'audio' }
  if (mime.startsWith('video/')) { return 'video' }
  if (mime.startsWith('text/') || mime.includes('json') || mime.includes('xml')) { return 'text' }
  return null
}

export const canPreviewDocument = (document = {}) => {
  if (!getPreviewType(document)) return false
  return !!(document.fileUrl || document.path)
}

export const isImageDocument = (document = {}) => getPreviewType(document) === 'image'

export const isPdfDocument = (document = {}) => getPreviewType(document) === 'pdf'

export const describeDocument = (document) => {
  if (!document) { return '' }
  const name = document.filename || document.name || ''
  const icon = getFileIcon({ name, mime: document.mimeType })
  return icon.label
}
