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MAAK,CACtJ,KAAK6B,YAAY,CAACH,OAAMC,OAAK,EAAE,QAAS7B,EAAIC,EAAUC,CAAgB,CACxE,EAEAgC,mBAAmBN,EAAcC,cAAQC,CAAuB9B,EAAiBC,EAAmBC,EAA8C,CAChJ,KAAK6B,YAAY,CAACH,OAAMC,OAAK,EAAE,UAAW7B,EAAIC,EAAUC,CAAgB,CAC1E,EAEQ6B,YAAYjC,EAA8BC,EAAsCC,EAAiBC,EAAW,IAAMC,EAAgD,SAAQ,CAChL,KAAKiC,YAAW,EAChB,KAAKxD,oBAAoByD,KAAK,CAACtC,UAASC,cAAaC,KAAIC,WAAUC,kBAAgB,CAAC,CACtF,EAEAiC,aAAW,CACL,KAAK/B,UACP,KAAKA,SAASE,QAAO,CAEzB,IAAA+B,CAAA,CAAAC,IAAA,UAAAC,CAAA,aAAAA,CAAA,EAtIW9E,GAAmB+E,EAAAC,CAAA,EAAAD,EAAAE,CAAA,EAAAF,EAAAG,CAAA,EAAAH,EAAAI,CAAA,EAAAJ,EAAAK,EAAA,GAAAL,EAAAM,CAAA,KAAAT,CAAA,CAAAU,KAAA,CAAAC,CAAA,EAAAC,KAAA,CAAnBxF,EAAmByF,QAAnBzF,EAAmB6E,KAAAa,WAFlB,MAAM,GAEd,GAAO1F,GAAP2F,QAAO3F,EAAmB,aAAA4F,CAAA,IAAAhB,CAAA","names":["NotificationService","messageTemplates","__async","_messageTemplates","snackbar","then","c","SnackbarTemplatesComponent","vc","createComponent","instance","constructor","usersService","platform","appRef","userInteractionService","swUpdate","blockUiService","notificationSubject","Subject","isBrowser","onUserAction$","subscribe","initSnackBar","isEnabled","versionUpdates","pipe","filter","evt","type","showWarningMessage","document","location","reload","delayWhen","debounceTime","_0","message","messageType","cb","duration","verticalPosition","tpls","snackBar","sb","dismiss","tpl","infoTpl","successTpl","warningTpl","errorTpl","loadingTpl","hide","openFromTemplate","afterDismissed","from","map","MatSnackBarModule","createNgModule","injector","switchMap","moduleRef","MatSnackBar","get","take","setViewContainerRef","showInfoMessage","text","title","$localize","showMessage","showSuccessMessage","showErrorMessage","showLoadingMessage","hideMessage","next","a","ɵfac","i","ɵɵinject","UsersService","Platform","ApplicationRef","UserInteractionService","SwUpdate","BlockUiService","ɵprov","j","token","factory","providedIn","_NotificationService","Z"],"ignoreList":[],"sources":["src/app/shared/services/notification/notification.service.ts"],"sourcesContent":["import { debounceTime, take, filter, delayWhen } from 'rxjs/operators';\r\nimport { UsersService } from '@shared/services/users/users.service';\r\nimport { Platform } from '@angular/cdk/platform';\r\nimport { Subject, Observable, from, map, switchMap } from 'rxjs';\r\nimport { createNgModule, Injectable, ApplicationRef, Optional, ViewContainerRef, EmbeddedViewRef } from '@angular/core';\r\nimport { MatSnackBar, MatSnackBarRef, MatSnackBarVerticalPosition } from '@angular/material/snack-bar';\r\nimport { UserInteractionService } from '../user-interaction/user-interaction.service';\r\nimport { SwUpdate, VersionReadyEvent } from '@angular/service-worker';\r\nimport type { SnackbarTemplatesComponent } from './snackbar-templates/snackbar-templates.component';\r\nimport { BlockUiService } from 'app/shared/components/block-ui/block-ui.service';\r\n\r\nexport type NotificationMessageType = 'info' | 'success' | 'warning' | 'error' | 'loading';\r\nexport type NotificationMessage = {\r\n  title: string;\r\n  text: string;\r\n}\r\n\r\nexport type NotificationArgs = {\r\n  message: NotificationMessage,\r\n  messageType: NotificationMessageType,\r\n  cb?: () => void,\r\n  duration?: number,\r\n  verticalPosition?: MatSnackBarVerticalPosition\r\n}\r\n\r\n@Injectable({\r\n  providedIn: 'root'\r\n})\r\nexport class NotificationService {\r\n  private notificationSubject: Subject<NotificationArgs> = new Subject();\r\n  private snackBar: MatSnackBar;\r\n  private vc: ViewContainerRef;\r\n\r\n\r\n  get messageTemplates(): Promise<SnackbarTemplatesComponent> {\r\n    return ( async () => {\r\n      if (!this._messageTemplates) {\r\n        const snackbar = await import('./snackbar-templates/snackbar-templates.component').then(c => c.SnackbarTemplatesComponent);\r\n        this._messageTemplates = this.vc.createComponent(snackbar).instance;\r\n      }\r\n      return this._messageTemplates;\r\n    })();\r\n\r\n  }\r\n  private _messageTemplates: SnackbarTemplatesComponent;\r\n\r\n  constructor(\r\n    public usersService: UsersService,\r\n    private platform: Platform,\r\n    private appRef: ApplicationRef,\r\n    private userInteractionService: UserInteractionService,\r\n    @Optional() private swUpdate: SwUpdate,\r\n    private blockUiService: BlockUiService\r\n  ) {\r\n    if (this.platform.isBrowser) {\r\n\r\n      this.userInteractionService.onUserAction$\r\n      .subscribe(() => {\r\n        this.initSnackBar().subscribe(() => {\r\n          if (this.swUpdate.isEnabled) {\r\n            this.swUpdate.versionUpdates\r\n            .pipe(filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'))\r\n            .subscribe(() => {\r\n              this.showWarningMessage($localize`New Application version is available. Reloading...`, null, () => {\r\n                document.location.reload();\r\n              });\r\n            });\r\n          }\r\n        });\r\n      });\r\n\r\n      this.notificationSubject\r\n      .pipe(\r\n        delayWhen(() => this.initSnackBar()),\r\n        debounceTime(500),\r\n      )\r\n      .subscribe(async ({message, messageType, cb, duration, verticalPosition}) => {\r\n        duration = duration > 0 ? duration : null;\r\n        const tpls = await this.messageTemplates;\r\n        const { snackBar } = this;\r\n        let sb: MatSnackBarRef<EmbeddedViewRef<any>>;\r\n        snackBar.dismiss();\r\n        tpls.message = message;\r\n        let tpl;\r\n        switch (messageType) {\r\n          case 'info':\r\n            tpl = tpls.infoTpl;\r\n          break;\r\n          case 'success':\r\n            tpl = tpls.successTpl;\r\n          break;\r\n          case 'warning':\r\n            tpl = tpls.warningTpl;\r\n          break;\r\n          case 'error':\r\n            tpl = tpls.errorTpl;\r\n          break;\r\n          case 'loading':\r\n            tpl = tpls.loadingTpl;\r\n          break;\r\n        }\r\n        this.blockUiService?.hide();\r\n        sb = snackBar.openFromTemplate(tpl, { duration, verticalPosition});\r\n        if (cb) {\r\n          sb.afterDismissed().subscribe(() => cb());\r\n        }\r\n      });\r\n    }\r\n  }\r\n\r\n  initSnackBar(): Observable<boolean> {\r\n    return from(import('@angular/material/snack-bar'))\r\n    .pipe(\r\n      map(({MatSnackBarModule}) => {\r\n        return createNgModule(MatSnackBarModule, this.appRef.injector);\r\n      }),\r\n      switchMap(moduleRef => {\r\n        return from(import('@angular/material/snack-bar'))\r\n          .pipe(\r\n            map(({MatSnackBar}) => {\r\n              this.snackBar = moduleRef.injector.get<MatSnackBar>(MatSnackBar);\r\n              return true;\r\n            })\r\n          )\r\n      }),\r\n      take(1)\r\n    );\r\n  }\r\n\r\n  setViewContainerRef(vc: ViewContainerRef): void {\r\n    this.vc = vc;\r\n  }\r\n\r\n  showInfoMessage(text: string, title = $localize`Info`, cb?: () => void, duration?: number, verticalPosition?: MatSnackBarVerticalPosition): void {\r\n    this.showMessage({text, title},'info', cb, duration, verticalPosition);\r\n  }\r\n\r\n  showSuccessMessage(text: string, title = $localize`Success`, cb?: () => void, duration?: number, verticalPosition?: MatSnackBarVerticalPosition): void {\r\n    this.showMessage({text, title},'success', cb, duration, verticalPosition);\r\n  }\r\n\r\n  showWarningMessage(text: string, title = $localize`Warning`, cb?: () => void, duration: number = 6000, verticalPosition: MatSnackBarVerticalPosition = 'top'): void {\r\n    this.showMessage({text, title},'warning', cb, duration, verticalPosition);\r\n  }\r\n\r\n  showErrorMessage(text: string, title = $localize`Error`, cb?: () => void, duration: number = 6000, verticalPosition: MatSnackBarVerticalPosition = 'top'): void {\r\n    this.showMessage({text, title},'error', cb, duration, verticalPosition);\r\n  }\r\n\r\n  showLoadingMessage(text: string, title = $localize`Waiting...`, cb?: () => void, duration?: number, verticalPosition?: MatSnackBarVerticalPosition): void {\r\n    this.showMessage({text, title},'loading', cb, duration, verticalPosition);\r\n  }\r\n\r\n  private showMessage(message: NotificationMessage, messageType: NotificationMessageType, cb?: () => void, duration = 4000, verticalPosition: MatSnackBarVerticalPosition = 'bottom'): void {\r\n    this.hideMessage();\r\n    this.notificationSubject.next({message, messageType, cb, duration, verticalPosition});\r\n  }\r\n\r\n  hideMessage(): void {\r\n    if (this.snackBar) {\r\n      this.snackBar.dismiss();\r\n    }\r\n  }\r\n}\r\n"]}