var d = Object.defineProperty;
var h = (t, i, o) => i in t ? d(t, i, { enumerable: !0, configurable: !0, writable: !0, value: o }) : t[i] = o;
var e = (t, i, o) => h(t, typeof i != "symbol" ? i + "" : i, o);
import { ContentType as m } from "./index-Document.types.js";
import { ModalService as a } from "./index-ModalService.js";
import f from "./index-compressor.esm.js";
import { MediaItemTransformer as u } from "./index-MediaItemTransformer.js";
class C extends u {
  /**
   * Constructor for ImageCompressorTransformer.
   *
   * @param config - Configuration options for the transformer.
   */
  constructor(o) {
    super();
    /**
     * List of applicable file types for compression.
     */
    e(this, "applicableFileTypes", [m.IMAGE_ANY, m.IMAGE_JPEG, m.IMAGE_JPG, m.IMAGE_PNG]);
    /**
     * Promise resolver for the transform method.
     * @private
     */
    e(this, "resolveTransformPromise");
    /**
     * Options for the compressor.
     * @private
     */
    e(this, "compressorOptions");
    /**
     * Flag to show loading modal during compression.
     * @private
     */
    e(this, "showLoadingModalDuringCompression");
    /**
     * Transforms the given files by compressing them.
     *
     * @param files - Array of files to be compressed.
     * @returns A promise that resolves with the compressed files or null.
     */
    e(this, "transform", (o) => new Promise((r) => {
      this.resolveTransformPromise = r, this.compress(o);
    }));
    /**
     * Compresses the given files.
     *
     * @param files - Array of files to be compressed.
     * @private
     */
    e(this, "compress", (o) => {
      this.showLoadingModalDuringCompression && a.openLoadingModal(), o.length === 1 ? this.compressSingleFile(o[0]) : this.compressMultipeFiles(o);
    });
    /**
     * Compresses a single file.
     *
     * @param selectedFile - The file to be compressed.
     * @private
     */
    e(this, "compressSingleFile", (o) => {
      const r = performance.now();
      this.compressorOptions.success = (n) => {
        var s;
        this.handleLoadingModalClose(r), (s = this.resolveTransformPromise) == null || s.call(this, [{
          file: n,
          filePriority: o.filePriority
        }]), this.resolveTransformPromise = void 0;
      }, new f(o.file, this.compressorOptions);
    });
    /**
     * Compresses multiple files.
     *
     * @param selectedFiles - Array of files to be compressed.
     * @private
     */
    e(this, "compressMultipeFiles", (o) => {
      const r = performance.now(), n = [];
      for (const s of o)
        n.push(new Promise((l, c) => {
          (!s.file || s.file.type && !this.applicableFileTypes.includes(s.file.type)) && l(s), this.compressorOptions.success = (p) => {
            l({
              file: p,
              filePriority: s.filePriority
            });
          }, this.compressorOptions.error = (p) => {
            c(p);
          }, new f(s.file, this.compressorOptions);
        }));
      Promise.all(n).then((s) => {
        var l;
        this.handleLoadingModalClose(r), (l = this.resolveTransformPromise) == null || l.call(this, s), this.resolveTransformPromise = void 0;
      });
    });
    /**
     * Handles the closing of the loading modal after compression.
     *
     * @param t0 - The timestamp when the compression started.
     *
     * This function calculates the duration of the compression process and determines
     * an appropriate timeout before closing the loading modal. The timeout is used to
     * ensure that the modal is visible for a minimum amount of time, even if the
     * compression process is very fast.
     *
     * Timeout logic:
     * - If the duration is less than or equal to 500ms, the timeout is set to 1500ms.
     * - If the duration is less than or equal to 1000ms, the timeout is set to 1000ms.
     * - If the duration is less than or equal to 1500ms, the timeout is set to 500ms.
     * - If the duration is less than or equal to 2000ms, the timeout is set to 0ms.
     *
     * After the calculated timeout, the loading modal is closed using the ModalService.
     */
    e(this, "handleLoadingModalClose", (o) => {
      if (this.showLoadingModalDuringCompression) {
        const r = performance.now(), n = r - o;
        let s = 1500;
        r - o <= 500 ? s = 1500 : n <= 1e3 ? s = 1e3 : n <= 1500 ? s = 500 : n <= 2e3 && (s = 0), setTimeout(() => {
          a.closeLoadingModal();
        }, s);
      }
    });
    this.compressorOptions = o.compressorOptions, this.showLoadingModalDuringCompression = o.showLoadingModalDuringCompression ?? !1;
  }
}
export {
  C as ImageCompressorTransformer
};
