import { v4 as uuid } from "uuid";
import { DialogType } from "../components/ez-dialog/DialogType";
import initI18n from "./i18n";
export default class ApplicationUtils {
  static async getDefaultMessageOptions() {
    if (!this.i18n) {
      this.i18n = await initI18n();
    }
    return {
      canClose: true,
      labelCancel: this.i18n("app.no"),
      labelConfirm: this.i18n("app.yes"),
      btnConfirmDanger: false
    };
  }
  static async showDialog(title, message, icon = null, confirm, dialogType = DialogType.DEFAULT, options) {
    if (options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = Object.assign(Object.assign({}, defaultMessageOptions), options);
    }
    return new Promise(resolve => {
      let dialog = document.createElement("ez-dialog");
      window.document.body.appendChild(dialog);
      dialog.show(title, message, dialogType, confirm, icon, options.labelCancel, options.labelConfirm, options.btnConfirmDanger, options.beforeClose)
        .then(ok => {
        dialog.remove();
        return resolve(ok);
      });
    });
  }
  static async alert(title, message, icon = null, options) {
    if (!options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = defaultMessageOptions;
    }
    return ApplicationUtils.showDialog(title, message, icon, false, DialogType.WARN, options);
  }
  static async error(title, message, icon = null, options) {
    if (!options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = defaultMessageOptions;
    }
    return ApplicationUtils.showDialog(title, message, icon, false, DialogType.CRITICAL, options);
  }
  static async success(title, message, icon = null, options) {
    if (!options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = defaultMessageOptions;
    }
    return ApplicationUtils.showDialog(title, message, icon, false, DialogType.SUCCESS, options);
  }
  static async confirm(title, message, icon = null, dialogType = DialogType.WARN, options) {
    if (!options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = defaultMessageOptions;
    }
    return ApplicationUtils.showDialog(title, message, icon, true, dialogType, options);
  }
  static async message(title, message, icon = null, options) {
    if (!options) {
      const defaultMessageOptions = await this.getDefaultMessageOptions();
      options = defaultMessageOptions;
    }
    return ApplicationUtils.showDialog(title, message, icon, false, DialogType.DEFAULT, options);
  }
  static async info(message, options) {
    const defaultMessageOptions = await this.getDefaultMessageOptions();
    if (options !== defaultMessageOptions) {
      options = Object.assign(Object.assign({}, defaultMessageOptions), options);
    }
    let useIcon = false;
    let toast = document.querySelector("ez-toast");
    if (!toast) {
      toast = document.createElement("ez-toast");
      const icon = document.createElement("ez-icon");
      icon.className = "ez-margin-right--small";
      icon.slot = "icon";
      icon.style.setProperty("--ez-icon--color", "var(--color--success)");
      toast.appendChild(icon);
      window.document.body.appendChild(toast);
    }
    if (options.iconName) {
      const iconElem = toast.querySelector("ez-icon");
      if (iconElem) {
        iconElem.iconName = options.iconName;
        useIcon = true;
      }
    }
    else {
      useIcon = false;
    }
    toast.show(message, 5000, useIcon, options.canClose);
  }
  static isModalContent(content) {
    return content instanceof HTMLElement && content.tagName === 'EZ-MODAL-CONTAINER';
  }
  /**
   * Exibe um popup configurado com os parâmetros informados e retorna um callBack
   * para removêlo da DOM quando necessário.
   *
   * @param popupProps
   */
  static async showPopup(popupProps) {
    var _a, _b;
    const popup = document.createElement("ez-popup");
    window.document.body.appendChild(popup);
    const { content } = popupProps;
    popup.setAttribute('id', uuid());
    popup.heightMode = (_a = popupProps.size) !== null && _a !== void 0 ? _a : "auto";
    popup.opened = true;
    if (ApplicationUtils.isModalContent(content)) {
      popup.appendChild(content);
      popup.useHeader = false;
    }
    else {
      popup.ezTitle = (_b = popupProps.title) !== null && _b !== void 0 ? _b : "";
      popup.innerHTML = content;
    }
    return () => popup.remove();
  }
  static async showModal(modalProps) {
    modalProps = Object.assign(Object.assign({}, ApplicationUtils.defaultModalProps), modalProps);
    const modal = document.createElement("ez-modal");
    window.document.body.appendChild(modal);
    modal.setAttribute('id', uuid());
    modal.modalSize = modalProps.size;
    modal.align = modalProps.position;
    modal.heightMode = modalProps.heightMode;
    modal.closeEsc = modalProps.closeEsc;
    modal.closeOutsideClick = modalProps.closeOutsideClick;
    modal.scrim = modalProps.useScrimLight ? 'light' : 'medium';
    if (modalProps.content instanceof String || typeof modalProps.content === 'string') {
      modal.innerHTML = modalProps.content;
    }
    else {
      modal.appendChild(modalProps.content);
    }
    modal.opened = true;
    return () => modal.remove();
  }
  static showAlerts(props) {
    const alertListId = 'EzAlertList';
    let alertList = document.getElementById(alertListId);
    if (!alertList) {
      alertList = document.createElement('ez-alert-list');
      alertList.setAttribute("id", alertListId);
      window.document.body.appendChild(alertList);
    }
    props = Object.assign(Object.assign({}, ApplicationUtils.defaultAlertListProps), props);
    alertList.itemRightSlotBuilder = props.itemRightSlotBuilder;
    alertList.enableDragAndDrop = props.enableDragAndDrop;
    alertList.enableExpand = props.enableExpand;
    alertList.opened = props.opened;
    alertList.alerts = props.alerts;
  }
}
ApplicationUtils.defaultModalProps = {
  content: null,
  position: 'right',
  size: 'small',
  heightMode: 'regular',
  closeOutsideClick: true,
  closeEsc: true
};
ApplicationUtils.defaultAlertListProps = {
  alerts: [],
  enableDragAndDrop: true,
  enableExpand: true,
  itemRightSlotBuilder: null,
  opened: true
};
;
