import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { filter, map, Observable } from 'rxjs';
import { REDDocumentViewer } from '../../pdf-viewer/services/document-viewer.service';

@Injectable()
export class DocumentUnloadedGuard implements CanDeactivate<unknown> {
    constructor(private readonly _documentViewer: REDDocumentViewer) {}

    canDeactivate(): Observable<boolean> {
        this._documentViewer.close();

        return this._documentViewer.loaded$.pipe(
            filter(loaded => !loaded),
            map(() => true),
        );
    }
}
