import type { FormField } from '@/components/FormParser/type'
import { showToast } from 'vant'

export const useImageUpload = function (field: FormField) {
  const { props } = field
  props.accept = 'image/*'
  if (props.limit > 1) {
    props.multiple = true
    props.maxCount = props.limit
    delete props.limit
  }
  if (props.size > 0) {
    props.maxSize = props.size * 1024 * 1024
    delete props.size
  }
  props.onOversize = () => {
    showToast(`文件大小不能超过 ${props.size}mb`)
  }
  props.beforeRead = (file: File) => {
    if (props.accept) {
      if (props.accept.includes(file.type)) {
        return true
      } else {
        showToast(`文件类型错误，请上传${props.accept}格式文件`)
        return false
      }
    }
  }
}
