import { isFunction } from "../_utils/is.mjs";
const requestUploadFile = (file, options) => {
  return new Promise((resolve) => {
    if (isFunction(options.uploadRequest)) {
      file.status = "uploading";
      file.request = options.uploadRequest({
        file,
        onProgress(percent) {
          file.percent = percent;
          if (isFunction(options.onProgress)) {
            options.onProgress(file);
          }
        },
        onSuccess() {
          file.status = "finished";
          file.retry = false;
          resolve(file);
          if (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 (isFunction(options.onError)) {
            options.onError(file);
          }
        }
      });
    } else {
      resolve(file);
    }
  });
};
const doUploadFile = (file, options) => {
  file.retry = false;
  file.message = "";
  if (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);
}
export {
  doUploadFile,
  doUploadFileList,
  generateImageDataUrl,
  isImageType,
  isPictureType,
  requestUploadFile
};
