import { ref as o } from "vue";
import "element-plus";
import y from "axios";
function v(m) {
  const a = o(!1), s = o(0), d = o(0), e = o("");
  return {
    uploading: a,
    // 是否上传中
    progress: s,
    // 上传进度
    currentFileName: e,
    // 当前文件名
    customHttpRequest: async (f) => {
      const { file: c, onSuccess: r, onError: l, onProgress: g, action: h, headers: w } = f;
      a.value = !0, s.value = 0, e.value = c.name;
      const u = new FormData();
      u.append("file", c);
      try {
        const n = await (m || y)({
          url: h,
          method: "post",
          data: u,
          timeout: 0,
          // 设置为0表示永不超时(当是网络不确定的情况下,比如下载大文件时,可以设置为0,不然会报错)
          headers: {
            "Content-Type": "multipart/form-data",
            ...w
            // 合并其他调用方自定义 header并显式类型化
          },
          // 上传进度事件：实时更新自定义进度条
          onUploadProgress: (t) => {
            if (t.total) {
              const x = Math.round(t.loaded / t.total * 100);
              s.value = x;
              const p = t;
              p.percent = t.total > 0 ? t.loaded / t.total * 100 : 0, g(p);
            }
          },
          // 下载进度事件（已挂载在 axios）
          onDownloadProgress: (t) => {
            t.total && (d.value = Math.round(t.loaded / t.total * 100));
          }
        });
        r == null || r(n.headers ? n.data : n);
      } catch (i) {
        l == null || l(i);
      } finally {
        a.value = !1, e.value = "";
      }
    },
    // 自定义上传逻辑
    downloadProgress: d
    // 下载进度
  };
}
export {
  v as useUpload
};
