import {Injectable} from '@angular/core'; import {Observable, Subject} from 'rxjs'; import {WindowService} from './window.service'; @Injectable({ providedIn: 'root' }) export class PatientContextService { private currentCpr: string | undefined; private currentEpisodeOfCare: string | undefined; private readonly innerCpr: Subject = new Subject(); private readonly innerEpisodeOfCare: Subject = new Subject(); constructor(private windowService: WindowService) { const cpr = this.windowService.nativeWindow().localStorage.getItem(`${this.windowService.nativeWindow().document.cookie}_patient_context_cpr`); const episodeOfCare = this.windowService.nativeWindow().localStorage.getItem(`${this.windowService.nativeWindow().document.cookie}_patient_context_eoc`); if (cpr) { this.setCpr(cpr); } if (episodeOfCare) { this.setEpisodeOfCare(episodeOfCare); } } public setCpr(cpr?: string): void { if (this.currentCpr !== cpr) { this.currentCpr = cpr; this.windowService.nativeWindow().localStorage.setItem( `${this.windowService.nativeWindow().document.cookie}_patient_context_cpr`, cpr || '' ); this.innerCpr.next(cpr); this.setEpisodeOfCare(undefined); } } public getCurrentCpr(): string | undefined { return this.currentCpr; } public setEpisodeOfCare(episodeOfCareIdentifier?: string): void { if (this.currentEpisodeOfCare !== episodeOfCareIdentifier) { this.currentEpisodeOfCare = episodeOfCareIdentifier; this.windowService.nativeWindow().localStorage.setItem( `${this.windowService.nativeWindow().document.cookie}_patient_context_eoc`, episodeOfCareIdentifier || '' ); this.innerEpisodeOfCare.next(episodeOfCareIdentifier); } } public getCurrentEpisodeOfCare(): string | undefined { return this.currentEpisodeOfCare; } get cpr$(): Observable { return this.innerCpr; } get episodeOfCare$(): Observable { return this.innerEpisodeOfCare; } }