var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
/** The `VegaImageUploaderPreviewController` class is responsible for showing a preview of an uploaded image in a modal. */
export class VegaImageUploaderPreviewController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * The `getImagePreviewURL` method is responsible for generating the URL of the image preview. It
     * checks if `this.value` exists, and if so, it returns the URL of the uploaded file (`(this.value as
     * VegaUploadedFile).url`). If the URL is not available, it falls back to creating a temporary URL
     * using `URL.createObjectURL(this.value as File)`. This temporary URL is used for previewing the
     * image until it is uploaded or saved permanently.
     *
     * @param {VegaFile} [file] the file object which needs to convert to a URL
     * @returns {Nullable<string>} the url of image
     */
    this.getImagePreviewURL = (file) => {
      file = file ? file : this.value;
      if (file) {
        return (file.url ||
          /*
           * The below method is e2e-test covered in
           *
           * @see{module:should-show-preview-image-when-click-the-preview-button-after-selected-image}
           */
          /* istanbul ignore next */
          URL.createObjectURL(file));
      }
    };
    /**
     * The `generatePreviewModal` method is a private method of the `VegaImageUploaderPreviewController`
     * class. It is responsible for creating a modal element and appending it to the document body.
     *
     * @param {Nullable<string>} imagePreviewURL the image url
     * @returns {HTMLElement} a preview vega modal
     */
    this.generatePreviewModal = (imagePreviewURL) => {
      // eslint-disable-next-line no-restricted-globals
      const previewModal = document.createElement('vega-modal');
      if (this.value && this.value.name) {
        previewModal.modalTitle = this.value.name;
      }
      // eslint-disable-next-line no-restricted-globals
      const container = document.createElement('div');
      container.style.textAlign = 'center';
      // eslint-disable-next-line no-restricted-globals
      const img = document.createElement('img');
      img.width = 0;
      img.className = 'preview-image';
      if (imagePreviewURL) {
        img.src = imagePreviewURL;
      }
      img.alt = '';
      img.style.width = '100%';
      container.appendChild(img);
      previewModal.appendChild(container);
      this.previewModalRef = previewModal;
      // eslint-disable-next-line no-restricted-globals
      document.body.append(previewModal);
      return previewModal;
    };
  }
  /**
   * remove preview modal when component destroy
   */
  disconnectedCallback() {
    var _a;
    (_a = this.previewModalRef) === null || _a === void 0 ? void 0 : _a.remove();
  }
  /**
   * The `showPreview` function displays an image preview in a modal, either by generating a new modal or updating an existing one.
   */
  showPreview() {
    const imagePreviewURL = this.getImagePreviewURL();
    if (!this.previewModalRef) {
      const previewModal = this.generatePreviewModal(imagePreviewURL);
      void previewModal.modal('show');
    }
    else {
      const previewImage = this.previewModalRef.querySelector('.preview-image');
      if (previewImage && imagePreviewURL) {
        previewImage.src = imagePreviewURL;
      }
      if (this.value && this.value.name) {
        this.previewModalRef.modalTitle = this.value.name;
      }
      void this.previewModalRef.modal('show');
    }
  }
}
__decorate([
  MapToComponentField()
], VegaImageUploaderPreviewController.prototype, "value", void 0);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaImageUploaderPreviewController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('getContentURL')
], VegaImageUploaderPreviewController.prototype, "getImagePreviewURL", void 0);
