import { getMediaFileType } from "./../_shared/get-media-file-type.js";
import { isAbsoluteUrl } from "./../_shared/media/is-absolute-url.js";
const MEDIA_SERVICES_URL = "/_serverless/ricos-media-services";
function getDownloadUrl(instance) {
  return async (fileId) => {
    const params = new URLSearchParams({ fileId });
    const response = await fetch(`${MEDIA_SERVICES_URL}/download-url?${params.toString()}`, {
      headers: { Authorization: instance }
    });
    const arrURLs = await response.json();
    return arrURLs;
  };
}
const getDomain = (mediaType, metaSiteId) => {
  const mediaTypeMapper = {
    picture: "static.wixstatic.com",
    video: "video.wixstatic.com",
    music: "music.wixstatic.com",
    document: `${metaSiteId}.usrfiles.com`,
    archive: `${metaSiteId}.usrfiles.com`
  };
  return mediaTypeMapper[mediaType];
};
function idContainsPrefix(id) {
  return id.includes("/");
}
function resolveFileUrl(metaSiteId, instance) {
  return ({ id, type, privacy }) => {
    if (id && isAbsoluteUrl(id)) {
      return Promise.resolve(id);
    }
    if (privacy !== "private" && idContainsPrefix(id ?? "")) {
      const domain = getDomain(getMediaFileType(type), metaSiteId);
      return Promise.resolve(`https://${domain}/${id}`);
    } else {
      return getDownloadUrl(instance)(id ?? "").then(
        (urls) => Array.isArray(urls) ? urls[0]?.path : ""
      );
    }
  };
}
export {
  resolveFileUrl
};
//# sourceMappingURL=resolve-file-url.js.map
