"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var media_manager_uploader_exports = {};
__export(media_manager_uploader_exports, {
  createUploadFn: () => createUploadFn
});
module.exports = __toCommonJS(media_manager_uploader_exports);
var import_consts = require("./consts.js");
const createUploadFn = (services) => {
  const consumer = services.editorProps.biSettings?.consumer;
  const { uid, siteOwnerId, getWixClientPromise } = services.wixInstance();
  const folderId = getFolderId(uid, siteOwnerId, services.editorProps.visitorUploads);
  return async (file, type, options) => {
    const { privacy = "public", onUploadProgress } = options || {};
    const params = new URLSearchParams({ mediaType: type, privacy });
    const wixClient = await getWixClientPromise();
    const response = await wixClient.fetchWithAuth(
      `${import_consts.MEDIA_SERVICES_URL}/upload-url?${params.toString()}`,
      { method: "POST", body: JSON.stringify({}) }
    );
    const responseData = await response.json();
    const uploadUrl = responseData.uploadUrl;
    const formData = new FormData();
    formData.append("file", file, file.name);
    formData.append("media_type", type);
    formData.append("acl", privacy);
    if (consumer) {
      formData.append("tags", `rich_content_${consumer}`);
    }
    if (folderId) {
      formData.append("parent_folder_id", folderId);
    }
    const requestConfig = {
      onUploadProgress: (progressEvent) => {
        onUploadProgress?.(progressEvent);
      }
    };
    const uploadResponse = await wixClient.fetchWithAuth(uploadUrl, {
      method: "POST",
      body: formData,
      ...requestConfig
    });
    const uploadRepsonseJson = await uploadResponse.json();
    const { error_description } = uploadRepsonseJson;
    return new Promise(
      (resolve, reject) => error_description ? reject(uploadRepsonseJson) : resolve(uploadRepsonseJson[0])
    );
  };
};
const getFolderId = (userId, siteOwnerId, visitorUploads) => {
  const isOwner = siteOwnerId === userId;
  const { folderId, shouldIncludeSiteOwner } = visitorUploads || {};
  const shouldUploadToFolder = shouldIncludeSiteOwner || !isOwner;
  return shouldUploadToFolder ? folderId : null;
};
//# sourceMappingURL=media-manager-uploader.js.map
