"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const is = require("../_utils/is.js");
const requestUploadFile = (file, options) => {
  return new Promise((resolve) => {
    if (is.isFunction(options.uploadRequest)) {
      file.status = "uploading";
      file.request = options.uploadRequest({
        file,
        onProgress(percent) {
          file.percent = percent;
          if (is.isFunction(options.onProgress)) {
            options.onProgress(file);
          }
        },
        onSuccess() {
          file.status = "finished";
          file.retry = false;
          resolve(file);
          if (is.isFunction(options.onSuccess)) {
            options.onSuccess(file);
          }
        },
        onError(response, retry) {
          file.status = "failed";
          file.message = response == null ? void 0 : response.message;
          file.retry = retry;
          if (file.percent) {
            file.percent = 0;
          }
          resolve(file);
          if (is.isFunction(options.onError)) {
            options.onError(file);
          }
        }
      });
    } else {
      resolve(file);
    }
  });
};
const doUploadFile = (file, options) => {
  file.retry = false;
  file.message = "";
  if (is.isFunction(options.onBeforeUpload)) {
    return options.onBeforeUpload(file).then((res) => {
      if (res === false) {
        return;
      }
      if (res instanceof File) {
        file.file = res;
      }
      return requestUploadFile(file, options);
    });
  } else {
    return requestUploadFile(file, options);
  }
};
const doUploadFileList = (fileList, options) => {
  if (fileList.length === 0) {
    return;
  }
  const rlt = fileList.map((f) => {
    if (f.status && ["finished", "uploading"].includes(f.status)) {
      return Promise.resolve(f);
    }
    return doUploadFile(f, options);
  });
  return Promise.allSettled(rlt).then(() => {
    return fileList;
  });
};
function isImageType(file) {
  var _a;
  return (_a = file.type) == null ? void 0 : _a.includes("image/");
}
function generateImageDataUrl(file) {
  if (typeof file === "string") {
    return file;
  }
  if (isImageType(file)) {
    return URL.createObjectURL(file);
  } else {
    return "";
  }
}
function isPictureType(type) {
  return !!type && ["picture", "picture-card"].includes(type);
}
exports.doUploadFile = doUploadFile;
exports.doUploadFileList = doUploadFileList;
exports.generateImageDataUrl = generateImageDataUrl;
exports.isImageType = isImageType;
exports.isPictureType = isPictureType;
exports.requestUploadFile = requestUploadFile;
