import axios from 'axios';
import { ref } from 'vue';
import '../../../api/resource/controller/index.mjs';
import { fileValidType, fileMd5 } from '../../../utils/src/file.mjs';
import { GmMessage } from '../../../components/src/plugins/message/index.mjs';
import { useGlobalConfig } from '../useGlobalConfig/index.mjs';
import { genFileId } from 'element-plus';
import { postResourceV1ResourcesUrl } from '../../../api/resource/controller/ZiYuanGuanLi/postResourceV1ResourcesUrl.mjs';

const useUploadFile = () => {
  const isLoading = ref(false);
  const domainCode = useGlobalConfig("domainCode");
  const sceneCode = useGlobalConfig("sceneCode");
  const abortControllers = ref(/* @__PURE__ */ new Map());
  const uploadFile = async (file, options) => {
    const uploadDomainCode = options?.domainCode ?? domainCode.value;
    const uploadSceneCode = (options?.sceneCode ?? sceneCode.value) || "internal_oss";
    if (!uploadDomainCode) {
      GmMessage.error("\u9886\u57DF\u7F16\u7801\u4E0D\u80FD\u4E3A\u7A7A");
      throw new Error("\u9886\u57DF\u7F16\u7801\u4E0D\u80FD\u4E3A\u7A7A");
    }
    const abortController = new AbortController();
    const fileKey = file.uid ?? genFileId();
    abortControllers.value.set(fileKey, abortController);
    try {
      isLoading.value = true;
      if (options?.accept && options?.accept !== "*") {
        const isValidType = fileValidType(file, options.accept);
        if (!isValidType) {
          GmMessage.error(`\u6587\u4EF6\u7C7B\u578B\u4E0D\u652F\u6301\uFF0C\u8BF7\u4E0A\u4F20 ${options.accept} \u683C\u5F0F\u7684\u6587\u4EF6`);
          throw new Error(`\u6587\u4EF6\u7C7B\u578B\u4E0D\u652F\u6301\uFF0C\u8BF7\u4E0A\u4F20 ${options.accept} \u683C\u5F0F\u7684\u6587\u4EF6`);
        }
      }
      const md5 = await fileMd5(file);
      file.md5 = md5;
      const acl = options?.acl || "default";
      const contentType = file.type || "application/octet-stream";
      const renameValue = options?.rename ?? 1;
      const { data } = await postResourceV1ResourcesUrl({
        domainCode: uploadDomainCode,
        // 领域编码
        sceneCode: uploadSceneCode,
        // 场景编码
        fileName: file.name,
        contentType,
        md5,
        size: file.size,
        acl,
        // 文件权限
        rename: renameValue
      });
      if (renameValue === 0 && data.data.url) {
        data.data.url = decodeURIComponent(data.data.url);
      }
      if (data.data.uploadUrl) {
        try {
          await axios.put(data.data.uploadUrl, file, {
            headers: {
              "Content-Type": contentType,
              "x-oss-object-acl ": acl,
              "Cache-Control": "max-age=2592000"
            },
            signal: abortController.signal,
            onUploadProgress: (progressEvent) => {
              const progressEvt = progressEvent.event;
              progressEvt.percent = progressEvt.total > 0 ? progressEvt.loaded / progressEvt.total * 100 : 0;
              options?.elOptions?.onProgress(progressEvent.event);
            }
          });
        } catch (error) {
          if (axios.isCancel(error) || error?.name === "AbortError") {
            console.log("\u4E0A\u4F20\u8BF7\u6C42\u5DF2\u53D6\u6D88");
            throw error;
          }
          GmMessage.error("\u63A8\u9001\u81F3\u5B58\u50A8\u6876\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5");
          throw error;
        }
      }
      return data.data;
    } finally {
      abortControllers.value.delete(fileKey);
      isLoading.value = false;
    }
  };
  const cancelUpload = (fileUid) => {
    const abortController = abortControllers.value.get(fileUid);
    if (abortController) {
      abortController.abort();
      abortControllers.value.delete(fileUid);
    }
  };
  const cancelAllUploads = () => {
    abortControllers.value.forEach((controller) => {
      controller.abort();
    });
    abortControllers.value.clear();
  };
  return {
    isLoading,
    uploadFile,
    cancelUpload,
    cancelAllUploads
  };
};

export { useUploadFile };
//# sourceMappingURL=index.mjs.map
