"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const MAX_WIDTH = 1920;
const MAX_HEIGHT = 1080;
function compress(src, el, isError, onLoad, setIsLoaded) {
  if (el.naturalHeight <= MAX_HEIGHT && el.naturalWidth <= MAX_WIDTH) {
    if (el.naturalHeight !== 0) {
      el.style.visibility = "visible";
      onLoad == null ? void 0 : onLoad();
      setIsLoaded == null ? void 0 : setIsLoaded(true);
    }
    if (isError) {
      el.src = src;
    }
    return;
  }
  const image = new Image();
  image.addEventListener("load", () => {
    const canvas = document.createElement("canvas");
    if (image.height > MAX_HEIGHT && image.height >= image.width) {
      image.width *= MAX_HEIGHT / image.height;
      image.height = MAX_HEIGHT;
    }
    if (image.width > MAX_HEIGHT && image.width > image.height) {
      image.height *= MAX_HEIGHT / image.width;
      image.width = MAX_HEIGHT;
    }
    const ctx = canvas.getContext("2d");
    canvas.width = image.width;
    canvas.height = image.height;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(image, 0, 0, image.width, image.height);
    const src2 = canvas.toDataURL("image/png");
    el.src = src2;
    onLoad == null ? void 0 : onLoad();
    setIsLoaded == null ? void 0 : setIsLoaded(true);
  });
  image.setAttribute("crossOrigin", "anonymous");
  image.src = src;
}
exports.MAX_HEIGHT = MAX_HEIGHT;
exports.MAX_WIDTH = MAX_WIDTH;
exports.default = compress;
//# sourceMappingURL=compress.js.map
