"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 create_media_upload_fn_exports = {};
__export(create_media_upload_fn_exports, {
  createMediaUploadFn: () => createMediaUploadFn
});
module.exports = __toCommonJS(create_media_upload_fn_exports);
var import_ricos_content = require("./../ricos-content/index.js");
var import_get_highest_quality_video = require("./get-highest-quality-video.js");
var import_get_media_file_type = require("./get-media-file-type.js");
var import_get_upload_error = require("./get-upload-error.js");
var import_consts = require("./request-service-provider/consts.js");
var import_media_manager_uploader = require("./request-service-provider/media-manager-uploader.js");
var import_with_file_size_limitation = require("./with-file-size-limitation.js");
const createMediaUploadFn = (services) => {
  const { metaSiteId, uid, siteOwnerId } = services.wixInstance();
  if (!metaSiteId) {
    return void 0;
  }
  const upload = (0, import_media_manager_uploader.createUploadFn)(services);
  const handleUploadError = (error, updateEntity) => {
    const isOwner = siteOwnerId === uid;
    const mediaUploadError = (0, import_get_upload_error.getUploadError)(error, metaSiteId, isOwner);
    updateEntity({ error: mediaUploadError });
  };
  const imageUpload = (file, updateEntity) => {
    return upload(file, import_consts.MediaType.IMAGE).then(({ file_name, original_file_name, height, width }) => {
      updateEntity({
        data: {
          id: file_name,
          height,
          width,
          original_file_name,
          file_name
        }
      });
    }).catch((error) => {
      return handleUploadError(error, updateEntity);
    });
  };
  const videoUpload = (file, updateEntity) => {
    return upload(file, import_consts.MediaType.VIDEO).then(({ file_output: { video, image } }) => {
      updateEntity({
        data: {
          pathname: (0, import_get_highest_quality_video.getHighestQualityVideo)(video).url,
          thumbnail: {
            pathname: image[0].url,
            width: image[0].width,
            height: image[0].height
          },
          title: file.name
        }
      });
    }).catch((error) => handleUploadError(error, updateEntity));
  };
  const getUnsupportedExtensionError = (fileName) => {
    return {
      key: import_ricos_content.MediaUploadErrorKey.UNSUPPORTED_EXTENSION,
      args: { extension: fileName.split(".").pop()?.toLowerCase() || "" }
    };
  };
  const galleryUpload = (file, updateEntity) => {
    const fileType = (0, import_get_media_file_type.getMediaFileType)(file.name);
    if (fileType === import_consts.MediaType.IMAGE) {
      return imageUpload(file, updateEntity);
    } else if (fileType === import_consts.MediaType.VIDEO) {
      return videoUpload(file, updateEntity);
    } else {
      return updateEntity({
        error: getUnsupportedExtensionError(file.name)
      });
    }
  };
  return (0, import_with_file_size_limitation.withFileSizeLimitation)(galleryUpload, services.editorProps.fileUploadSizeLimit);
};
//# sourceMappingURL=create-media-upload-fn.js.map
