import {
  createUploadEndBIData,
  createUploadStartBIData
} from "../../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 = createUploadStartBIData(type, fileSize, fileType);
      return {
        start: () => this.publishers.byTopic("ricos.upload.functionality.uploadStarted").publish(uploadStartBiData),
        success: () => {
          this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish(createUploadEndBIData(uploadStartBiData));
        },
        error: (error) => {
          this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish(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 = 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 = createUploadEndBIData(uploadStartBiData, error);
        this.publishers.byTopic("ricos.upload.functionality.uploadFinished").publish(uploadFinishedBiData);
      }
    } catch (e) {
      this.notifyError(e);
    }
  }
}
export {
  UploadService
};
//# sourceMappingURL=upload-service.js.map
