"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 upload_service_exports = {};
__export(upload_service_exports, {
  UploadService: () => UploadService
});
module.exports = __toCommonJS(upload_service_exports);
var import_media_upload_bi = require("../../ricos-common/biCallbacks/media-upload-bi.js");
class UploadService {
  constructor(updateService) {
    this.topicsToPublish = [
      "ricos.upload.functionality.uploadStarted",
      "ricos.upload.functionality.uploadFinished"
    ];
    this.trackUploadBi = (type, fileSize, fileType) => {
      const uploadStartBiData = (0, import_media_upload_bi.createUploadStartBIData)(type, fileSize, fileType);
      return {
        start: () => this.publishers.byTopic("ricos.upload.functionality.uploadStarted").publish(uploadStartBiData),
        success: () => {
          this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish((0, import_media_upload_bi.createUploadEndBIData)(uploadStartBiData));
        },
        error: (error) => {
          this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish((0, import_media_upload_bi.createUploadEndBIData)(uploadStartBiData, error));
        }
      };
    };
    this.updateService = updateService;
    this.onInputChange = null;
  }
  setErrorNotifier(getErrorNotifer) {
    this.getErrorNotifier = getErrorNotifer;
  }
  setHiddenInputElement(getHiddenInputElement) {
    this.getHiddenInputElement = getHiddenInputElement;
  }
  notifyError(error) {
    this.getErrorNotifier().notify(error);
  }
  /** @deprecated use selectFiles */
  selectFilesCallback(accept = "image/*", multiple, callback) {
    const hiddenInputElement = this.getHiddenInputElement();
    this.onInputChange && hiddenInputElement.removeEventListener("change", this.onInputChange);
    this.onInputChange = (event) => {
      const files = Array.from(event.target.files || []);
      event.target.value = "";
      callback(files);
    };
    hiddenInputElement.addEventListener("change", this.onInputChange, { once: true });
    hiddenInputElement.accept = accept;
    hiddenInputElement.multiple = !!multiple;
    hiddenInputElement.click();
  }
  selectFiles({
    accept = "image/*",
    multiple = true
  }) {
    const that = this;
    return new Promise((resolve) => {
      const hiddenInputElement = that.getHiddenInputElement();
      that.onInputChange && hiddenInputElement.removeEventListener("change", that.onInputChange);
      that.onInputChange = (event) => {
        const files = Array.from(event.target.files || []);
        event.target.value = "";
        resolve(files);
      };
      hiddenInputElement.addEventListener("change", that.onInputChange, { once: true });
      hiddenInputElement.accept = accept;
      hiddenInputElement.multiple = !!multiple;
      hiddenInputElement.click();
    });
  }
  async uploadFile(file, nodeId, uploader, type, mediaPluginService, fileState) {
    try {
      const url = URL.createObjectURL(file);
      await Promise.resolve();
      const newFileState = this.updateService.updateLoadingState(
        url,
        file,
        nodeId,
        type,
        mediaPluginService,
        fileState
      );
      const uploadStartBiData = (0, import_media_upload_bi.createUploadStartBIData)(type, file.size, file.type);
      this.publishers.byTopic("ricos.upload.functionality.uploadStarted").publish(uploadStartBiData);
      let error;
      try {
        const uploadedFile = await uploader.upload(file);
        this.updateService.updatePluginData(
          uploadedFile,
          nodeId,
          type,
          mediaPluginService,
          newFileState
        );
        URL.revokeObjectURL(url);
      } catch (e) {
        error = e;
        this.notifyError(e);
        this.updateService.updateErrorState(e, nodeId, type, mediaPluginService, newFileState);
      } finally {
        const uploadFinishedBiData = (0, import_media_upload_bi.createUploadEndBIData)(uploadStartBiData, error);
        this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish(uploadFinishedBiData);
      }
    } catch (e) {
      this.notifyError(e);
    }
  }
}
//# sourceMappingURL=upload-service.js.map
