import { FileListType } from '@/components/Z3upFiles/data'

// 上传文件自动归类
type FileObjType = {
  [key: string]: string[]
}
export const fileImgArr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp', 'svg', 'psd', 'ai']
export const fileVideoArr = ['mp4', 'mov', 'avi', 'mkv', 'flv', 'wmv', 'mpeg', 'webm']

const fileObj: FileObjType = {
  img: fileImgArr,
  video: fileVideoArr,
  model: ['4dage', 'obj', 'stl', 'fbx', 'gltf', '3ds', 'blend', 'dae', 'step'],
  audio: ['mp3', 'wav', 'flac', 'aac', 'ogg', 'm4a'],
  doc: ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'csv', 'md', 'html']
  // 'other':[]
}

export const fileTypeRes = (fileName: string) => {
  const txtArr = fileName.split('.')
  const txt = txtArr[txtArr.length - 1]

  let type = 'other'

  for (const k in fileObj) {
    if (fileObj[k].includes(txt.toLowerCase())) type = k
  }
  return type
}

// 处理附件id顺序-把图片放在前面
export const fileIdsResFu = (listTemp: FileListType[]) => {
  const files = listTemp || []
  const fileImgArr = files.filter(v => v.type === 'img')
  const fileOtherArr = files.filter(v => v.type !== 'img')
  const fileIds = [...fileImgArr, ...fileOtherArr].map(v => v.id).join(',')
  return fileIds
}
