import{h}from"@stencil/core";import{ToastType,ToastMessageList,ToastrConfig,WindowService}from"../../../../../api/dist/ontotext-workbench-api";import{sanitizeHTML}from"../../utils/html-utils";import{TranslationService}from"../../services/translation.service";const toastTypeToIconMap={[ToastType.INFO]:"ri-information-2-fill",[ToastType.SUCCESS]:"ri-checkbox-circle-line",[ToastType.WARNING]:"ri-message-line",[ToastType.ERROR]:"ri-alert-line"};export class OntoToastr{constructor(){this.toasts=new ToastMessageList([]),this.toastToTimeout=new Map,this.config=ToastrConfig.getDefaultConfig(),this.onToastMouseEnter=t=>()=>{this.clearToastTimeout(t)},this.onToastMouseLeave=t=>()=>{this.setTimeoutForToast(t)}}handleToastCreate(t){this.addToast(t.detail)}render(){var t;return h("section",{key:"21b2b0dd0f50aabbe2eed7ee6080eaf450c167e7",style:{display:this.toasts.isEmpty()?"none":"flex"},class:`onto-toastr-container ${this.config.position}`},null===(t=this.toasts)||void 0===t?void 0:t.getItems().map(t=>{var e,s,o;return h("div",{class:`onto-toast toast ${t.type}`,key:t.id,onMouseEnter:this.onToastMouseEnter(t),onMouseLeave:this.onToastMouseLeave(t),onClick:this.handleToastClick(t),onKeyUp:this.handleKeyUp(t)},h("i",{class:`${toastTypeToIconMap[t.type]}`}),h("span",null,(null===(e=t.config)||void 0===e?void 0:e.title)?h("div",{class:"toast-title",innerHTML:sanitizeHTML(TranslationService.translate(t.config.title,null===(s=t.config)||void 0===s?void 0:s.translationParams))}):"",h("div",{class:"toast-message",innerHTML:sanitizeHTML(TranslationService.translate(t.message,null===(o=t.config)||void 0===o?void 0:o.translationParams))})))}))}addToast(t){this.toasts.addToStart(t),this.setTimeoutForToast(t),this.updateToastrReference()}setTimeoutForToast(t){var e;const s=WindowService.getWindow().setTimeout(()=>{this.toasts.remove(t),this.clearToastTimeout(t)},(null===(e=t.config)||void 0===e?void 0:e.timeout)||this.config.timeout);this.toastToTimeout.set(t,s)}updateToastrReference(){this.toasts=new ToastMessageList(this.toasts.getItems())}clearToastTimeout(t){const e=this.toastToTimeout.get(t);clearTimeout(e),this.toastToTimeout.delete(t),this.updateToastrReference()}handleToastClick(t){return e=>{var s,o;(null===(s=t.config)||void 0===s?void 0:s.onClick)&&t.config.onClick(e),(null===(o=t.config)||void 0===o?void 0:o.removeOnClick)&&(this.toasts.remove(t),this.clearToastTimeout(t))}}handleKeyUp(t){return e=>{"Enter"===e.key&&this.handleToastClick(t)}}static get is(){return"onto-toastr"}static get originalStyleUrls(){return{$:["onto-toastr.scss"]}}static get styleUrls(){return{$:["onto-toastr.css"]}}static get states(){return{toasts:{}}}static get listeners(){return[{name:"create-toast-event",method:"handleToastCreate",target:"body",capture:!1,passive:!1}]}}