<script setup lang="ts">
import mime from 'mime'
import { useFileInfoStore } from '@/stores/file-info-data'

const { mimeFileMapping } = useFileInfoStore()

defineProps<{
  fileMimeType: string | undefined
  fileName: string | undefined
  size: string
}>()

function getIcon(mimeType: string | undefined, fileName: string | undefined) {
  if (!mimeType) return 'mdi-file-question-outline'

  const extension = mime.getExtension(mimeType)

  if (extension && mimeFileMapping[mimeType]) {
    return mimeFileMapping[mimeType].icon
  }

  if (fileName && fileName.includes('.')) return 'mdi-file-outline'

  return 'mdi-file-question-outline'
}

function getColor(mimeType: string | undefined, fileName: string | undefined) {
  if (mimeType === undefined) return 'grey-5'

  const extension = mime.getExtension(mimeType)

  if (extension && mimeFileMapping[mimeType]) {
    return mimeFileMapping[mimeType].color
  }
  if (fileName && fileName.includes('.')) return 'blue-11'

  return 'grey-5'
}

function getIconSize(size: string) {
  const sizeMapping = {
    preview: '6em',
    list: '2em',
  }
  return sizeMapping[size as keyof typeof sizeMapping]
}
</script>

<template>
  <q-icon
    :name="fileMimeType && getIcon(fileMimeType, fileName)"
    :color="fileMimeType && getColor(fileMimeType, fileName)"
    :size="getIconSize(size)"
  />
</template>
