function s(a) {
  return new Promise((r, t) => {
    const e = new FileReader();
    e.onerror = () => t(e.error ?? new Error("Failed to read image file.")), e.onload = () => r(typeof e.result == "string" ? e.result : ""), e.readAsDataURL(a);
  });
}
function d(a) {
  return new Promise((r, t) => {
    const e = new Image();
    e.onerror = () => t(new Error("Failed to load image file.")), e.onload = () => r(e), e.src = a;
  });
}
function l(a, r) {
  const t = Math.min(1, 1500 / Math.max(a, r));
  return {
    height: Math.max(1, Math.round(r * t)),
    width: Math.max(1, Math.round(a * t))
  };
}
async function c(a) {
  var i;
  const r = await s(a);
  if (!a.type.startsWith("image/") || globalThis.document === void 0) return r;
  const t = await d(r), {
    height: e,
    width: o
  } = l(t.naturalWidth || t.width, t.naturalHeight || t.height), n = globalThis.document.createElement("canvas");
  return n.width = o, n.height = e, (i = n.getContext("2d")) == null || i.drawImage(t, 0, 0, o, e), n.toDataURL("image/jpeg", 0.7);
}
export {
  c as compressBEditorImageFile
};
