"use strict";
function chooseFile({ accept = "*", multiple = false } = {}) {
  return new Promise((resolve) => {
    let input = document.createElement("input");
    input.setAttribute("type", "file");
    input.setAttribute("accept", accept);
    if (multiple) input.setAttribute("multiple", "multiple");
    input.onchange = (e) => {
      const files = e.target.files;
      console.log("\u{1F680} ~ chooseFile ~ files:", files);
      if (multiple) {
        resolve(Array.from(files));
      } else {
        resolve(files[0]);
      }
    };
    window.addEventListener(
      "focus",
      () => {
        console.log("close");
        resolve(null);
      },
      { once: true }
    );
    input.click();
  });
}
const getFileExtension = (name) => {
  return name.substring(name.lastIndexOf(".") + 1);
};
const getFileName = (name) => {
  return name.substring(0, name.lastIndexOf("."));
};
function formatBytes(bytes = 0) {
  if (!bytes) {
    return "0 B";
  }
  const unitArr = new Array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  let index = 0;
  index = Math.floor(Math.log(bytes) / Math.log(1024));
  var size = bytes / Math.pow(1024, index);
  size = size.toFixed(2);
  return size + " " + unitArr[index];
}
const getFileThumbByIconName = (name) => {
  return `https://resource.mini-enjoy.com/saas/images/files/${name}`;
};
const getFileThumbByFileExt = (_ext = "") => {
  let ext = _ext;
  if (ext[0] === ".") ext = ext.slice(1);
  const fun = getFileThumbByIconName;
  switch (ext) {
    case "txt":
      return fun("txt.svg");
    case "doc":
    case "docx":
      return fun("doc.svg");
    case "xls":
    case "xlsx":
      return fun("xls.svg");
    case "ppt":
    case "pptx":
      return fun("ppt.svg");
    case "p12":
      return fun("p12.svg");
    case "pem":
      return fun("pem.svg");
    case "rar":
    case "zip":
      return fun("compress.svg");
    case "mp4":
      return fun("video.svg");
    default:
      return fun("file.svg");
  }
};
const getFileThumbByFileName = (filename) => {
  return getFileThumbByFileExt(getFileExtension(filename));
};

export { chooseFile, formatBytes, getFileExtension, getFileName, getFileThumbByFileExt, getFileThumbByFileName, getFileThumbByIconName };
//# sourceMappingURL=file.mjs.map
