import * as i0 from '@angular/core';
import { Directive, Input, HostListener, NgModule, Injectable } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i1 from '@angular/router';
import { RouterModule } from '@angular/router';
import { __awaiter } from 'tslib';
import { Subject, take } from 'rxjs';

/**
 * Places a navigation lock on this page.
 *
 * Don't forget to add the following to your route:
 *
 * ```ts
 * canDeactivate: [BsNavigationLockGuard]
 * ```
 *
 * and implement the `BsHasNavigationLock` on the page:
 *
 * ```ts
 * ViewChild('navigationLock') navigationLock!: BsNavigationLockDirective;
 * ```
 *
 **/
class BsNavigationLockDirective {
    constructor(router, route) {
        // console.log('initial navigation', this.route.snapshot.pathFromRoot.flatMap(ars => ars.url));
        // combineLatest([this.route.fragment])
        //   .pipe(takeUntil(this.destroyed$))
        //   .subscribe((fragment) => {
        //     console.log('fragments', fragment);
        //   });
        this.router = router;
        this.route = route;
        this.destroyed$ = new Subject();
        // router.events.subscribe((ev) => {
        //   // if (ev instanceof RouterEvent) {
        //   //   (<RouterEvent>ev).
        //   // }
        //   // console.log('router event', ev);
        // });
    }
    requestCanExit() {
        return new Promise((resolve, reject) => {
            if (typeof this.canExit === 'undefined') {
                resolve(true);
            }
            else if (typeof this.canExit === 'boolean') {
                resolve(this.canExit);
            }
            else if (typeof this.canExit === 'function') {
                const result = this.canExit();
                resolve(result);
            }
            else {
                this.canExit.pipe(take(1))
                    .subscribe((result) => resolve(result));
            }
        });
    }
    onBeforeUnload(ev) {
        return __awaiter(this, void 0, void 0, function* () {
            const canExit = yield this.requestCanExit();
            if (!canExit) {
                ev.preventDefault();
                ev.returnValue = false;
                return 'Are you sure?';
            }
            else {
                return undefined;
            }
        });
    }
    onUnload(ev) {
    }
    ngOnDestroy() {
        this.destroyed$.next(true);
    }
}
BsNavigationLockDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockDirective, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }], target: i0.ɵɵFactoryTarget.Directive });
BsNavigationLockDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.7", type: BsNavigationLockDirective, selector: "[bsNavigationLock]", inputs: { canExit: "canExit", exitMessage: "exitMessage" }, host: { listeners: { "window:beforeunload": "onBeforeUnload($event)", "window:unload": "onUnload($event)" } }, exportAs: ["bsNavigationLock"], ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsNavigationLock]',
                    exportAs: 'bsNavigationLock'
                }]
        }], ctorParameters: function () { return [{ type: i1.Router }, { type: i1.ActivatedRoute }]; }, propDecorators: { canExit: [{
                type: Input
            }], exitMessage: [{
                type: Input
            }], onBeforeUnload: [{
                type: HostListener,
                args: ['window:beforeunload', ['$event']]
            }], onUnload: [{
                type: HostListener,
                args: ['window:unload', ['$event']]
            }] } });

class BsNavigationLockModule {
}
BsNavigationLockModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
BsNavigationLockModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockModule, declarations: [BsNavigationLockDirective], imports: [CommonModule,
        RouterModule], exports: [BsNavigationLockDirective] });
BsNavigationLockModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockModule, imports: [CommonModule,
        RouterModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        BsNavigationLockDirective
                    ],
                    imports: [
                        CommonModule,
                        RouterModule
                    ],
                    exports: [
                        BsNavigationLockDirective
                    ]
                }]
        }] });

class BsNavigationLockGuard {
    canDeactivate(component, currentRoute, currentState, nextState) {
        if (component.navigationLock) {
            return component.navigationLock.requestCanExit();
        }
        else {
            console.warn('When using <bs-navigation-lock>, you should implement BsHasNavigationLock and add "@ViewChild(\'navigationLock\') navigationLock!: BsNavigationLockDirective;" to your page');
            return new Promise(resolve => resolve(false));
        }
    }
}
BsNavigationLockGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockGuard, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
BsNavigationLockGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockGuard, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsNavigationLockGuard, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { BsNavigationLockDirective, BsNavigationLockGuard, BsNavigationLockModule };
//# sourceMappingURL=mintplayer-ng-bootstrap-navigation-lock.mjs.map
