import imageClientAPI from "image-client-api/dist/imageClientSDK.js";
import { GALLERY_ITEMS_TYPES } from "../../plugin-gallery/defaults.js";
import { getAbsoluteUrl } from "./get-absolute-url.js";
import { isAbsoluteUrl } from "./is-absolute-url.js";
import { WIX_MEDIA_BASE_URL } from "./media-url-consts.js";
const getResizedImageUrl = (item, originalUrl, resizeMethod, requiredWidth, requiredHeight, sharpParams, focalPoint) => {
  originalUrl = originalUrl || "";
  sharpParams = sharpParams || {};
  if (sharpParams.quality > 0) {
    sharpParams.quality = Math.min(90, sharpParams.quality);
  }
  const focalPointObj = { x: 50, y: 50 };
  if (focalPoint && focalPoint[0] >= 0 && focalPoint[1] >= 0) {
    focalPointObj.x = Math.round(focalPoint[0] * 100);
    focalPointObj.y = Math.round(focalPoint[1] * 100);
  }
  if (sharpParams.allowUsm === true && sharpParams.usm) {
    sharpParams.usm.usm_a = Math.min(5, Math.max(0, sharpParams.usm.usm_a || 0));
    sharpParams.usm.usm_r = Math.min(128, Math.max(0, sharpParams.usm.usm_r || 0));
    sharpParams.usm.usm_t = Math.min(1, Math.max(0, sharpParams.usm.usm_t || 0));
  } else {
    sharpParams.usm = {
      usm_a: 0,
      usm_r: 0,
      usm_t: 0
    };
  }
  const resizer = resizeMethod === "fit" ? imageClientAPI.getScaleToFitImageURL : imageClientAPI.getScaleToFillImageURL;
  const options = {};
  if (sharpParams.quality > 0) {
    options.quality = sharpParams.quality;
  }
  if (sharpParams.blur > 0) {
    options.filters = {
      blur: sharpParams.blur
    };
  }
  if (focalPointObj) {
    options.focalPoint = focalPointObj;
  }
  if (sharpParams && sharpParams.usm) {
    options.unsharpMask = {
      radius: parseFloat(sharpParams.usm.usm_r),
      amount: parseFloat(sharpParams.usm.usm_a),
      threshold: parseFloat(sharpParams.usm.usm_t)
    };
  }
  if (isAbsoluteUrl(originalUrl)) {
    return originalUrl;
  }
  return resizer(
    getWixFilename(originalUrl),
    item.maxWidth,
    item.maxHeight,
    requiredWidth,
    requiredHeight,
    options
  );
};
const resizeMediaUrl = ({
  item,
  originalUrl,
  resizeMethod,
  requiredWidth,
  requiredHeight,
  sharpParams,
  focalPoint
}) => {
  originalUrl = originalUrl || "";
  sharpParams = sharpParams || {};
  if (originalUrl.indexOf("base64") !== -1) {
    return originalUrl;
  }
  requiredWidth = Math.ceil(requiredWidth);
  requiredHeight = Math.ceil(requiredHeight);
  if (resizeMethod === GALLERY_ITEMS_TYPES.VIDEO) {
    return getAbsoluteUrl(originalUrl, GALLERY_ITEMS_TYPES.VIDEO);
  } else if (requiredWidth >= item.maxWidth && requiredHeight >= item.maxHeight) {
    const url = item.dto.metaData.type === GALLERY_ITEMS_TYPES.VIDEO ? originalUrl : item.url;
    return getAbsoluteUrl(url, GALLERY_ITEMS_TYPES.IMAGE);
  } else {
    return getResizedImageUrl(
      item,
      originalUrl,
      resizeMethod,
      requiredWidth,
      requiredHeight,
      sharpParams,
      focalPoint
    );
  }
};
const getWixFilename = (url) => url.replace(WIX_MEDIA_BASE_URL, "");
export {
  resizeMediaUrl
};
//# sourceMappingURL=resize-media-url.js.map
