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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { VegaInternalIconManager } from '../../../../helpers/icon/internal-icon-manager';
import trashCan from '../../../../global/icons/trash-can';
import arrowRotateRight from '../../../../global/icons/arrow-rotate-right';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaClick } from '../../../../helpers/event-manager/event-id/vega-event-id';
/** renders a list of files for a file uploader component. */
export class VegaFileUploaderFileListRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Used to trigger an event or action related to remove a specific file in the file uploader component
     *
     * @param {VegaFileUploaderFile} file the vega file object
     */
    this.fileRemoveHandler = (file) => {
      const removeEvent = this.removeEventEmitter.emit(file);
      if (!removeEvent.defaultPrevented) {
        this.vegaFileUploaderValueController.setValue(this.value.filter((item) => item !== file));
      }
    };
    /**
     * Used to trigger an event or action related to retrying the upload of a specific file in the file uploader component
     *
     * @param {VegaFileUploaderFile} file the vega file object
     */
    this.fileReUploadHandler = (file) => {
      this.retryEventEmitter.emit(file);
    };
    /**
     * Used to trigger an event or action related to preview of a specific file in the file uploader component
     *
     * @param {VegaFileUploaderFile} file the vega file object
     */
    this.filePreviewHandler = (file) => {
      this.previewEventEmitter.emit(file);
    };
  }
  /**
   * The function renders a list of file within a div element based on the value provided.
   *
   * @returns {HTMLElement} An HTMLElement containing a div element with class 'file-uploader-file-list' and
   * 'file-list-hidden' if the value is empty. If the value is not empty, it will map over the value
   * array and return a div element for each file with the file name displayed.
   */
  render() {
    const isFileListEmpty = !this.value || this.value.length < 1;
    return (h("div", { class: {
        'vega-file-uploader-file-list': true,
        'vega-file-list-hidden': isFileListEmpty,
      } }, !isFileListEmpty &&
      this.value.map((file) => {
        return (h("vega-box", { key: file.key, class: "vega-file-uploader-file", padding: "size-8", corners: "rounded-8" },
          h("vega-flex", { alignItems: "start", justifyContent: "space-between" },
            h("vega-flex", { class: "vega-file-status-and-name", gap: "size-8", alignItems: "start" },
              h("div", { style: { paddingTop: '3px' } }, this.vegaFileUploaderFileStatusRenderer.render(file.status, file.uploadingPercent)),
              h("vega-flex", { gap: "size-4", direction: "col" },
                h("vega-font", { color: file.status === 'done' || file.status === 'error'
                    ? 'text-primary'
                    : 'text-secondary', variant: "font-field-label" }, file.name),
                file.status === 'error' && file.errorMessage && (h("vega-font", { color: "text-error", variant: "font-footnote1-short" }, file.errorMessage)))),
            h("vega-flex", { gap: "size-4", class: "vega-file-list-buttons", justifyContent: "end" },
              file.previewable && (h("vega-button-circle", { class: "vega-file-preview-button", variant: "icon-only", size: "small", icon: "zoom-in", iconColor: "text-secondary", label: "Preview", tooltip: { disableInteractive: true }, ref: (button) => {
                  DomNodeSubjectObserverFactory.addUniqueObserverToNode(button, VegaClick, () => {
                    this.filePreviewHandler(file);
                  });
                } })),
              file.status === 'error' && (h("vega-button-circle", { variant: "icon-only", size: "small", icon: VegaInternalIconManager.getIconKey('arrow-rotate-right'), iconColor: "text-secondary", label: "Retry", tooltip: { disableInteractive: true }, disabled: this.disabled, ref: (button) => {
                  DomNodeSubjectObserverFactory.addUniqueObserverToNode(button, VegaClick, () => {
                    this.fileReUploadHandler(file);
                  });
                } })),
              h("vega-button-circle", { variant: "icon-only", size: "small", icon: VegaInternalIconManager.getIconKey('trash-can'), iconColor: "text-secondary", label: "Delete", tooltip: { disableInteractive: true }, disabled: this.disabled, ref: (button) => {
                  DomNodeSubjectObserverFactory.addUniqueObserverToNode(button, VegaClick, () => {
                    this.fileRemoveHandler(file);
                  });
                } })))));
      })));
  }
}
(() => {
  VegaInternalIconManager.register({
    'trash-can': trashCan,
    'arrow-rotate-right': arrowRotateRight,
  });
})();
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "vegaFileUploaderFileStatusRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "removeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "vegaFileUploaderValueController", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "retryEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaFileUploaderFileListRenderer.prototype, "previewEventEmitter", void 0);
