import Vue from 'vue'
import { anonymization } from './anonymization'
export const dcmUpload = async function (data, config, progressFn, fileInfo) {
  return new Promise(async resolve => {
    try {
      // let blob = await encoder(file, config)
      let blob = await fileToBlob(data.file)
      if (config) {
        blob = await anonymization(data.file, config)
      }

      let res = await Vue.prototype.OSSclient.multipartUpload(Object.assign(data, { file: blob.blob }), progressFn, fileInfo)
      resolve({
        ...res,
        image: blob.pixelDataElement
      })
      // let OSSclientA = await OSSclient
      // let blob = await encoder(file)
      // let res = await OSSclient.put(name, blob.blob)


    } catch (e) {
      console.log(data.file, 'warning')
      resolve(false)
      console.log(e)
    }
  })
}
function fileToBlob(file) {
  // 创建 FileReader 对象
  let reader = new FileReader();
  return new Promise(resolve => {
    // FileReader 添加 load 事件
    reader.addEventListener('load', (e) => {
      let blob;
      if (typeof e.target.result === 'object') {
        blob = new Blob([e.target.result])
      } else {
        blob = e.target.result
      }
      resolve({ blob })
    })
    // FileReader 以 ArrayBuffer 格式 读取 File 对象中数据
    reader.readAsArrayBuffer(file)
  })
}
