import request from '@/utils/request'
import { uuid } from '@/utils/utils'

const uploadApi = '/common/upload'

export default {
  uploadApi,
  basePath: import.meta.env.VITE_BASE_PATH,
  uploadFunc: (file: any, onUploadProgress: any): Promise<any> => {
    const formData = new FormData()
    formData.append('file', file)

    return request
      .post(uploadApi, formData, {
        onUploadProgress
      })
      .then((res: any) => {
        return { data: res.data }
      })
  },
  transformData: (data: string) => {
    if (typeof data === 'string') {
      // 将逗号分隔的字符串转换为数组
      return data
        .split(',')
        .filter((item: string) => item)
        .map(x => ({
          key: uuid(),
          url: x,
          status: 'success',
          name: x.split('/').pop()
        }))
    } else if (Array.isArray(data)) {
      return data
    } else {
      return []
    }
  }
}
