import { __awaiter, __generator } from 'tslib';
import { Injectable, ɵɵdefineInjectable, EventEmitter, Component, Input, Output, ViewChild, InjectionToken, Inject, ɵɵinject, NgModule } from '@angular/core';
import { Validators, FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { Platform, ModalController } from '@ionic/angular';
import { TapClientError } from '@iotize/tap/client/impl';
import { ConnectionState } from '@iotize/tap/protocol/api';
import { CurrentDeviceService, TaskManagerService, LibCommonModule, TaskManagerUiModule } from '@iotize/ionic';
import { of, from } from 'rxjs';
import { map } from 'rxjs/operators';
import { createDebugger } from '@iotize/common/debug';

/**
 * @fileoverview added by tsickle
 * Generated from: lib/tap-auth.service.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
var TapAuthService = /** @class */ (function () {
    function TapAuthService() {
        try {
            this.options = JSON.parse(localStorage.getItem('TapAuthServiceStorage') || '{}');
        }
        catch (err) {
            this.options = {};
        }
        if (!this.options) {
            this.options = {};
        }
    }
    /**
     * @return {?}
     */
    TapAuthService.prototype.store = /**
     * @return {?}
     */
    function () {
        localStorage.setItem('TapAuthServiceStorage', JSON.stringify(this.options));
    };
    TapAuthService.decorators = [
        { type: Injectable, args: [{
                    providedIn: 'root'
                },] }
    ];
    /** @nocollapse */
    TapAuthService.ctorParameters = function () { return []; };
    /** @nocollapse */ TapAuthService.ɵprov = ɵɵdefineInjectable({ factory: function TapAuthService_Factory() { return new TapAuthService(); }, token: TapAuthService, providedIn: "root" });
    return TapAuthService;
}());
if (false) {
    /** @type {?} */
    TapAuthService.prototype.options;
}

/**
 * @fileoverview added by tsickle
 * Generated from: lib/device-login/device-login.component.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
/**
 * @record
 */
function LoginOption() { }
if (false) {
    /** @type {?} */
    LoginOption.prototype.username;
    /** @type {?} */
    LoginOption.prototype.password;
    /** @type {?} */
    LoginOption.prototype.rememberMe;
    /** @type {?} */
    LoginOption.prototype.autoLogin;
}
var DeviceLoginComponent = /** @class */ (function () {
    function DeviceLoginComponent(tapService, router, platform, modalController, formBuilder, tapAuth) {
        this.tapService = tapService;
        this.router = router;
        this.platform = platform;
        this.modalController = modalController;
        this.formBuilder = formBuilder;
        this.tapAuth = tapAuth;
        /**
         * Refresh session state on login
         */
        this.refreshSessionState = true;
        this.header = true;
        this.footer = true;
        this.returnUrl = undefined;
        this.closeButton = true;
        this.backButton = false;
        this.enableAutoLogin = false;
        this.enableRememberMe = true;
        this.title = 'Tap login';
        this.onError = new EventEmitter();
        this.promise$ = undefined;
        this.loading = false;
        /** @type {?} */
        var defaultUsername = this.tapAuth.options.lastCredentials ? this.tapAuth.options.lastCredentials.username : '';
        /** @type {?} */
        var defaultPassword = '';
        this.form = this.formBuilder.group({
            username: [defaultUsername, Validators.required],
            password: [defaultPassword],
            rememberMe: [this.tapAuth.options.rememberMe],
            autoLogin: [this.tapAuth.options.autoLogin]
        });
    }
    Object.defineProperty(DeviceLoginComponent.prototype, "tap", {
        // unregisterBackAction: any;
        get: 
        // unregisterBackAction: any;
        /**
         * @return {?}
         */
        function () {
            return this.tapService.tap;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DeviceLoginComponent.prototype, "rememberMe", {
        get: /**
         * @return {?}
         */
        function () {
            return this.getField('rememberMe');
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DeviceLoginComponent.prototype, "autoLogin", {
        get: /**
         * @return {?}
         */
        function () {
            return this.getField('autoLogin');
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DeviceLoginComponent.prototype, "username", {
        get: /**
         * @return {?}
         */
        function () {
            return this.getField('username');
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DeviceLoginComponent.prototype, "password", {
        get: /**
         * @return {?}
         */
        function () {
            return this.getField('password');
        },
        enumerable: true,
        configurable: true
    });
    /**
     * @param {?} name
     * @return {?}
     */
    DeviceLoginComponent.prototype.getField = /**
     * @param {?} name
     * @return {?}
     */
    function (name) {
        /** @type {?} */
        var field = this.form.get(name);
        if (!field) {
            throw new Error("Internal error: missing input with [formControlName]=\"" + name + "\" in html template");
        }
        return field;
    };
    /**
     * @return {?}
     */
    DeviceLoginComponent.prototype.ngOnInit = /**
     * @return {?}
     */
    function () {
        var _this = this;
        this.sessionState$ = this.tap.auth.sessionState;
        this.connectionStateSub = this.tapService.connectionState.subscribe((/**
         * @param {?} event
         * @return {?}
         */
        function (event) {
            if (event.newState === ConnectionState.CONNECTED) {
                if (_this.pendingLoginCall) {
                    _this._execCall(_this.pendingLoginCall);
                }
            }
        }));
        // this.unregisterBackAction = this.platform.registerBackButtonAction(() => {
        //   this.dismiss();
        // })
    };
    // ngOnDestroy() {
    //   // if (this.unregisterBackAction) this.unregisterBackAction();
    // }
    // ngOnDestroy() {
    //   // if (this.unregisterBackAction) this.unregisterBackAction();
    // }
    /**
     * @return {?}
     */
    DeviceLoginComponent.prototype.login = 
    // ngOnDestroy() {
    //   // if (this.unregisterBackAction) this.unregisterBackAction();
    // }
    /**
     * @return {?}
     */
    function () {
        return __awaiter(this, void 0, void 0, function () {
            var username, password, rememberMe, autoLogin, options, loginCall;
            var _this = this;
            return __generator(this, function (_a) {
                this.loading = true;
                username = this.username.value;
                password = this.password.value;
                rememberMe = this.rememberMe.value;
                autoLogin = this.autoLogin.value;
                options = { username: username, password: password, rememberMe: rememberMe, autoLogin: autoLogin };
                loginCall = (/**
                 * @return {?}
                 */
                function () { return __awaiter(_this, void 0, void 0, function () {
                    return __generator(this, function (_a) {
                        switch (_a.label) {
                            case 0: return [4 /*yield*/, this.tapService.login(username, password, this.refreshSessionState)];
                            case 1:
                                _a.sent();
                                return [2 /*return*/, options];
                        }
                    });
                }); });
                this._execCall(loginCall);
                return [2 /*return*/];
            });
        });
    };
    /**
     * @return {?}
     */
    DeviceLoginComponent.prototype.dismiss = /**
     * @return {?}
     */
    function () {
        return __awaiter(this, void 0, void 0, function () {
            var topOverlay;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.modalController.getTop()];
                    case 1:
                        topOverlay = _a.sent();
                        if (topOverlay) {
                            this.modalController.dismiss(topOverlay.id);
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @return {?}
     */
    DeviceLoginComponent.prototype.cancelAction = /**
     * @return {?}
     */
    function () {
        this.pendingLoginCall = undefined;
        this.error = undefined;
        this.form.enable();
    };
    /**
     * @private
     * @param {?} call
     * @return {?}
     */
    DeviceLoginComponent.prototype._execCall = /**
     * @private
     * @param {?} call
     * @return {?}
     */
    function (call) {
        var _this = this;
        this.loading = true;
        return call()
            .then((/**
         * @param {?} options
         * @return {?}
         */
        function (options) {
            _this.tapAuth.options = {
                autoLogin: options.autoLogin,
                rememberMe: options.rememberMe
            };
            if (options.rememberMe) {
                _this.tapAuth.options.lastCredentials = {
                    username: options.username
                    // password: options.password
                };
            }
            else {
                _this.tapAuth.options.lastCredentials = undefined;
                _this.tapAuth.options.autoLogin = false;
            }
            _this.tapAuth.store();
            if (_this.returnUrl) {
                _this.router.navigateByUrl(_this.returnUrl);
            }
            _this.pendingLoginCall = undefined;
            _this.form.enable();
            _this.dismiss();
        }))
            .catch((/**
         * @param {?} err
         * @return {?}
         */
        function (err) {
            if (err.code === TapClientError.Code.NotConnectedError
                || err.code === 'NfcTagLostError'
                || err.code === 'NfcNotConnectedError') {
                _this.pendingLoginCall = call;
                _this.form.disable();
            }
            else {
                _this._onError(err);
            }
        }))
            .then((/**
         * @return {?}
         */
        function () {
            _this.loading = false;
        }));
    };
    /**
     * @private
     * @param {?} err
     * @return {?}
     */
    DeviceLoginComponent.prototype._onError = /**
     * @private
     * @param {?} err
     * @return {?}
     */
    function (err) {
        this.error = err;
        this.onError.emit(err);
    };
    DeviceLoginComponent.decorators = [
        { type: Component, args: [{
                    selector: 'tap-device-login',
                    template: "<ion-header *ngIf=\"header\">\r\n    <ion-toolbar color=\"primary\">\r\n        <ion-buttons slot=\"start\">\r\n            <ion-back-button *ngIf=\"backButton\" [defaultHref]=\"'/device'\"></ion-back-button>\r\n            <ion-button *ngIf=\"closeButton\" (click)=\"dismiss()\">\r\n                <ion-icon name=\"arrow-back\"></ion-icon>\r\n            </ion-button>\r\n        </ion-buttons>\r\n        <ion-title class=\"text-center\">\r\n            {{title}}\r\n        </ion-title>\r\n    </ion-toolbar>\r\n</ion-header>\r\n<ion-content>\r\n    <ion-progress-bar type=\"indeterminate\" [hidden]=\"!loading\"></ion-progress-bar>\r\n    <ion-grid>\r\n        <ion-row>\r\n            <ion-col class=\"ion-align-self-center\">\r\n                <iotize-feedback-error [error]=\"error\"></iotize-feedback-error>\r\n                <form (ngSubmit)=\"login()\" [formGroup]=\"form\" (keydown.enter)=\"login()\">\r\n                    <div class=\"ion-padding\">\r\n                        <ion-item>\r\n                            <ion-input type=\"text\" [formControlName]=\"'username'\" placeholder=\"Username\"></ion-input>\r\n                        </ion-item>\r\n                        <ion-item>\r\n                            <ion-input type=\"password\" [formControlName]=\"'password'\" placeholder=\"Password\"></ion-input>\r\n                        </ion-item>\r\n                        <ion-item *ngIf=\"enableRememberMe\">\r\n                            <ion-label>Remember me</ion-label>\r\n                            <ion-toggle [formControlName]=\"'rememberMe'\"></ion-toggle>\r\n                        </ion-item>\r\n                        <ion-item *ngIf=\"rememberMe.value && enableAutoLogin\">\r\n                            <ion-label>Automatic login on connection</ion-label>\r\n                            <ion-toggle [formControlName]=\"'autoLogin'\"></ion-toggle>\r\n                        </ion-item>\r\n                    </div>\r\n                    <ion-item lines=\"none\">\r\n                        <ion-button *ngIf=\"pendingLoginCall\" slot=\"end\" (click)=\"cancelAction()\" color=\"warning\" size=\"default\">\r\n                            <ion-icon name=\"close\"></ion-icon>\r\n                            Cancel\r\n                        </ion-button>\r\n                        <ion-button slot=\"end\" type=\"submit\" [disabled]=\"loading || pendingLoginCall\" size=\"default\">\r\n                            <ion-icon name=\"log-in\"></ion-icon>\r\n                            Login\r\n                        </ion-button>\r\n                    </ion-item>\r\n                    <ion-item lines=\"none\" *ngIf=\"pendingLoginCall\">\r\n                        <ion-text class=\"ion-text-wrap\" slot=\"end\" color=\"warning\">\r\n                            <ion-icon name=\"alert\"></ion-icon>\r\n                            Please reconnect Tap to login\r\n                        </ion-text>\r\n                    </ion-item>\r\n                </form>\r\n            </ion-col>\r\n        </ion-row>\r\n    </ion-grid>\r\n</ion-content>\r\n<ion-footer *ngIf=\"footer\"></ion-footer>\r\n",
                    styles: ["ion-grid{height:100%;justify-content:center}"]
                }] }
    ];
    /** @nocollapse */
    DeviceLoginComponent.ctorParameters = function () { return [
        { type: CurrentDeviceService },
        { type: Router },
        { type: Platform },
        { type: ModalController },
        { type: FormBuilder },
        { type: TapAuthService }
    ]; };
    DeviceLoginComponent.propDecorators = {
        refreshSessionState: [{ type: Input }],
        header: [{ type: Input }],
        footer: [{ type: Input }],
        returnUrl: [{ type: Input }],
        closeButton: [{ type: Input }],
        backButton: [{ type: Input }],
        enableAutoLogin: [{ type: Input }],
        enableRememberMe: [{ type: Input }],
        title: [{ type: Input }],
        onError: [{ type: Output }]
    };
    return DeviceLoginComponent;
}());
if (false) {
    /**
     * Refresh session state on login
     * @type {?}
     */
    DeviceLoginComponent.prototype.refreshSessionState;
    /** @type {?} */
    DeviceLoginComponent.prototype.header;
    /** @type {?} */
    DeviceLoginComponent.prototype.footer;
    /** @type {?} */
    DeviceLoginComponent.prototype.returnUrl;
    /** @type {?} */
    DeviceLoginComponent.prototype.closeButton;
    /** @type {?} */
    DeviceLoginComponent.prototype.backButton;
    /** @type {?} */
    DeviceLoginComponent.prototype.enableAutoLogin;
    /** @type {?} */
    DeviceLoginComponent.prototype.enableRememberMe;
    /** @type {?} */
    DeviceLoginComponent.prototype.title;
    /** @type {?} */
    DeviceLoginComponent.prototype.onError;
    /** @type {?} */
    DeviceLoginComponent.prototype.form;
    /** @type {?} */
    DeviceLoginComponent.prototype.error;
    /** @type {?} */
    DeviceLoginComponent.prototype.sessionState$;
    /** @type {?} */
    DeviceLoginComponent.prototype.promise$;
    /** @type {?} */
    DeviceLoginComponent.prototype.loading;
    /** @type {?} */
    DeviceLoginComponent.prototype.pendingLoginCall;
    /** @type {?} */
    DeviceLoginComponent.prototype.connectionStateSub;
    /**
     * @type {?}
     * @protected
     */
    DeviceLoginComponent.prototype.tapService;
    /**
     * @type {?}
     * @private
     */
    DeviceLoginComponent.prototype.router;
    /**
     * @type {?}
     * @private
     */
    DeviceLoginComponent.prototype.platform;
    /** @type {?} */
    DeviceLoginComponent.prototype.modalController;
    /**
     * @type {?}
     * @private
     */
    DeviceLoginComponent.prototype.formBuilder;
    /**
     * @type {?}
     * @private
     */
    DeviceLoginComponent.prototype.tapAuth;
}

/**
 * @fileoverview added by tsickle
 * Generated from: lib/change-password/change-password.component.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
/** @type {?} */
var TASK_ID = 'change-user-password';
var ChangePasswordComponent = /** @class */ (function () {
    function ChangePasswordComponent(currentDevice, modalController, formBuilder, taskManager) {
        var _this = this;
        this.currentDevice = currentDevice;
        this.modalController = modalController;
        this.formBuilder = formBuilder;
        this.taskManager = taskManager;
        this.confirmPassword = false;
        this.askCurrentPassword = false;
        this.header = true;
        this.userId = undefined;
        this.loading = false;
        this.form = this.formBuilder.group({
            password: ['', Validators.required],
            confirmPassword: [''],
            currentPassword: [''],
        });
        this.changePasswordTask = this.taskManager.createTask({
            id: TASK_ID,
            info: {
                title: 'Change user password',
                feedback: 'User password changed!'
            },
            exec: (/**
             * @return {?}
             */
            function () {
                return _this.changePassword();
            })
        });
    }
    Object.defineProperty(ChangePasswordComponent.prototype, "tap", {
        get: /**
         * @return {?}
         */
        function () {
            return this.currentDevice.tap;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ChangePasswordComponent.prototype, "password", {
        get: /**
         * @return {?}
         */
        function () {
            return (/** @type {?} */ (this.form.get('password')));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ChangePasswordComponent.prototype, "currentPassword", {
        get: /**
         * @return {?}
         */
        function () {
            return (/** @type {?} */ (this.form.get('currentPassword')));
        },
        enumerable: true,
        configurable: true
    });
    /**
     * @return {?}
     */
    ChangePasswordComponent.prototype.ngOnInit = /**
     * @return {?}
     */
    function () {
    };
    /**
     * @return {?}
     */
    ChangePasswordComponent.prototype.submit = /**
     * @return {?}
     */
    function () {
        this.error = undefined;
        this.changePasswordTaskComponent.execTask();
    };
    /**
     * @return {?}
     */
    ChangePasswordComponent.prototype.changePassword = /**
     * @return {?}
     */
    function () {
        return __awaiter(this, void 0, void 0, function () {
            var newPassword, err_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 4, , 5]);
                        newPassword = this.password.value;
                        if (!this.askCurrentPassword) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.tap.auth.login({
                                username: this.tap.auth.sessionStateSnapshot.name,
                                password: this.currentPassword.value
                            })];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2:
                        this.promise$ = this.tap.auth.changePassword(newPassword, this.userId);
                        return [4 /*yield*/, this.promise$];
                    case 3:
                        _a.sent();
                        this.modalController.dismiss();
                        return [3 /*break*/, 5];
                    case 4:
                        err_1 = _a.sent();
                        this.showError(err_1);
                        return [3 /*break*/, 5];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @return {?}
     */
    ChangePasswordComponent.prototype.dismiss = /**
     * @return {?}
     */
    function () {
        this.modalController.dismiss();
    };
    /**
     * @param {?} err
     * @return {?}
     */
    ChangePasswordComponent.prototype.showError = /**
     * @param {?} err
     * @return {?}
     */
    function (err) {
        this.error = err;
    };
    ChangePasswordComponent.decorators = [
        { type: Component, args: [{
                    selector: 'tap-change-password',
                    template: "<ion-header *ngIf=\"header\">\r\n\t<ion-toolbar color=\"primary\">\r\n\t\t<ion-buttons slot=\"start\">\r\n\t\t\t<ion-button (click)=\"modalController.dismiss()\">\r\n\t\t\t  <ion-icon name=\"arrow-back\"></ion-icon>\r\n\t\t\t</ion-button>\r\n\t\t</ion-buttons>\r\n\t\t<ion-title class=\"text-center\">\r\n\t\t\tChange profile password\r\n\t\t</ion-title>\r\n\t</ion-toolbar>\r\n</ion-header>\r\n<ion-content>\r\n\t<ion-grid>\r\n\t\t<ion-row>\r\n\t\t\t<ion-col class=\"ion-align-self-center\">\r\n\t\t\t\t<iotize-feedback-error [error]=\"error\"></iotize-feedback-error>\r\n\t\t\t\t<form (ngSubmit)=\"submit()\" [formGroup]=\"form\" (keydown.enter)=\"submit()\">\r\n\t\t\t\t\t<ion-item *ngIf=\"askCurrentPassword\">\r\n\t\t\t\t\t\t<ion-input [formControlName]=\"'currentPassword'\" placeholder=\"Current password\" required type=\"password\"></ion-input>\r\n\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t<ion-item>\r\n\t\t\t\t\t\t<ion-input [formControlName]=\"'password'\" placeholder=\"New password\" required type=\"password\"></ion-input>\r\n\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t<ion-item *ngIf=\"confirmPassword\">\r\n\t\t\t\t\t\t<ion-input [formControlName]=\"'confirmPassword'\" placeholder=\"Confirm new password\" required type=\"password\"></ion-input>\r\n\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t<app-task-manager #changePasswordTaskComponent [task]=\"changePasswordTask\">\r\n\t\t\t\t\t\t<app-task-manager-action>\r\n\t\t\t\t\t\t\t<ion-item lines=\"none\">\r\n\t\t\t\t\t\t\t\t<ion-button size=\"default\" slot=\"end\" [disabled]=\"form['invalid']\" expand=\"block\" type=\"submit\">\r\n\t\t\t\t\t\t\t\t\t<ion-icon name=\"create\"></ion-icon>\r\n\t\t\t\t\t\t\t\t\tChange password\r\n\t\t\t\t\t\t\t\t</ion-button>\r\n\t\t\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t\t</app-task-manager-action>\r\n\t\t\t\t\t\t<app-task-manager-delayed>\r\n\t\t\t\t\t\t\t<ion-item lines=\"none\">\r\n\t\t\t\t\t\t\t\t<ion-text class=\"ion-text-wrap\" slot=\"end\" color=\"warning\">\r\n\t\t\t\t\t\t\t\t\t<ion-icon name=\"alert\"></ion-icon>\r\n\t\t\t\t\t\t\t\t\tPlease reconnect Tap to change password\r\n\t\t\t\t\t\t\t\t</ion-text>\r\n\t\t\t\t\t\t\t\t<ion-button size=\"default\" color=\"warning\" app-task-manager-action-cancel>\r\n\t\t\t\t\t\t\t\t\t<ion-icon name=\"close\"></ion-icon>\r\n\t\t\t\t\t\t\t\t\tCancel\r\n\t\t\t\t\t\t\t\t</ion-button>\r\n\t\t\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t\t</app-task-manager-delayed>\r\n\t\t\t\t\t\t<app-task-manager-running>\r\n\t\t\t\t\t\t\t<ion-item lines=\"none\">\r\n\t\t\t\t\t\t\t\t<ion-progress-bar type=\"indeterminate\"></ion-progress-bar>\r\n\t\t\t\t\t\t\t</ion-item>\r\n\t\t\t\t\t\t</app-task-manager-running>\r\n\t\t\t\t\t</app-task-manager>\r\n\t\t\t\t</form>\r\n\t\t\t</ion-col>\r\n\t\t</ion-row>\r\n\t</ion-grid>\r\n</ion-content>\r\n",
                    styles: [""]
                }] }
    ];
    /** @nocollapse */
    ChangePasswordComponent.ctorParameters = function () { return [
        { type: CurrentDeviceService },
        { type: ModalController },
        { type: FormBuilder },
        { type: TaskManagerService }
    ]; };
    ChangePasswordComponent.propDecorators = {
        changePasswordTaskComponent: [{ type: ViewChild, args: ['changePasswordTaskComponent', { static: true },] }],
        confirmPassword: [{ type: Input }],
        askCurrentPassword: [{ type: Input }],
        header: [{ type: Input }],
        userId: [{ type: Input }]
    };
    return ChangePasswordComponent;
}());
if (false) {
    /** @type {?} */
    ChangePasswordComponent.prototype.changePasswordTaskComponent;
    /** @type {?} */
    ChangePasswordComponent.prototype.confirmPassword;
    /** @type {?} */
    ChangePasswordComponent.prototype.askCurrentPassword;
    /** @type {?} */
    ChangePasswordComponent.prototype.header;
    /** @type {?} */
    ChangePasswordComponent.prototype.userId;
    /** @type {?} */
    ChangePasswordComponent.prototype.form;
    /** @type {?} */
    ChangePasswordComponent.prototype.promise$;
    /** @type {?} */
    ChangePasswordComponent.prototype.error;
    /** @type {?} */
    ChangePasswordComponent.prototype.loading;
    /** @type {?} */
    ChangePasswordComponent.prototype.changePasswordTask;
    /**
     * @type {?}
     * @protected
     */
    ChangePasswordComponent.prototype.currentDevice;
    /** @type {?} */
    ChangePasswordComponent.prototype.modalController;
    /**
     * @type {?}
     * @private
     */
    ChangePasswordComponent.prototype.formBuilder;
    /**
     * @type {?}
     * @private
     */
    ChangePasswordComponent.prototype.taskManager;
}

/**
 * @fileoverview added by tsickle
 * Generated from: lib/debug.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
/** @type {?} */
var debug = createDebugger('@iotize/ionic:auth');

/**
 * @fileoverview added by tsickle
 * Generated from: lib/device-auth.guard.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
/** @type {?} */
var DEVICE_AUTH_GUARD_CONFIG = new InjectionToken('DEVICE_AUTH_GUARD_CONFIG');
/**
 * @record
 */
function DeviceAuthGuardRouteData() { }
if (false) {
    /** @type {?|undefined} */
    DeviceAuthGuardRouteData.prototype.disableAuthGuardOnPage;
    /** @type {?|undefined} */
    DeviceAuthGuardRouteData.prototype.username;
}
/**
 * @record
 */
function DeviceAuthGuardConfig() { }
if (false) {
    /** @type {?} */
    DeviceAuthGuardConfig.prototype.loginUrl;
}
var DeviceAuthGuard = /** @class */ (function () {
    // private routerData?: DeviceAuthGuardRouteData;
    function DeviceAuthGuard(tapService, router, route, config) {
        this.tapService = tapService;
        this.router = router;
        this.route = route;
        this.config = config;
        this.subscriptions = {};
        // this.setupSessionStateListener();
    }
    /**
     * @param {?} component
     * @param {?} currentRoute
     * @param {?} currentState
     * @param {?=} nextState
     * @return {?}
     */
    DeviceAuthGuard.prototype.canDeactivate = /**
     * @param {?} component
     * @param {?} currentRoute
     * @param {?} currentState
     * @param {?=} nextState
     * @return {?}
     */
    function (component, currentRoute, currentState, nextState) {
        // if (this.sessionStateSub) {
        //   this.sessionStateSub.unsubscribe();
        // }
        return true;
    };
    /**
     * @param {?} route
     * @param {?} state
     * @return {?}
     */
    DeviceAuthGuard.prototype.canActivate = /**
     * @param {?} route
     * @param {?} state
     * @return {?}
     */
    function (route, state) {
        var _this = this;
        this.setupSessionStateListener(route, state);
        if (!route.data.username) {
            return of(true);
        }
        debug("Check is login as \"" + route.data.username + "\" to access \"" + route.url + "\"");
        return from(this.tapService
            .isLoggedInAs(route.data.username))
            .pipe(map((/**
         * @param {?} isLoggedAs
         * @return {?}
         */
        function (isLoggedAs) {
            if (!isLoggedAs) {
                _this.onUnauthorized(state.url, route);
            }
            return isLoggedAs;
        })));
    };
    /**
     * @private
     * @param {?} returnUrl
     * @param {?} activatedRoute
     * @return {?}
     */
    DeviceAuthGuard.prototype.onUnauthorized = /**
     * @private
     * @param {?} returnUrl
     * @param {?} activatedRoute
     * @return {?}
     */
    function (returnUrl, activatedRoute) {
        debug("Not authorized to access this route \"" + activatedRoute.url + "\" -> redirect to \"" + this.config.loginUrl + "\"");
        this.router.navigate([this.config.loginUrl], { queryParams: { returnUrl: returnUrl } });
    };
    /**
     * Listen to session state changes for the given route. It it's the active route, we redirect to login
     * @param activatedRoute
     * @param state
     */
    /**
     * Listen to session state changes for the given route. It it's the active route, we redirect to login
     * @param {?} activatedRoute
     * @param {?} state
     * @return {?}
     */
    DeviceAuthGuard.prototype.setupSessionStateListener = /**
     * Listen to session state changes for the given route. It it's the active route, we redirect to login
     * @param {?} activatedRoute
     * @param {?} state
     * @return {?}
     */
    function (activatedRoute, state) {
        var _this = this;
        if (state.url in this.subscriptions) {
            return this.subscriptions[state.url];
        }
        /** @type {?} */
        var subscription = this.tapService
            .sessionState
            .subscribe((/**
         * @param {?} event
         * @return {?}
         */
        function (event) {
            if (_this.router.isActive(state.url, true)) {
                /** @type {?} */
                var routeData = activatedRoute.data;
                if (routeData && routeData.username && !routeData.disableAuthGuardOnPage) {
                    /** @type {?} */
                    var usernames = Array.isArray(routeData.username)
                        ? routeData.username
                        : [routeData.username];
                    debug('DeviceAuthGuard', 'Active route: ', state.url, 'requires username(s)', usernames, "Current username: " + event.name);
                    if (usernames.indexOf(event.name) === -1) {
                        _this.onUnauthorized(state.url, activatedRoute);
                    }
                }
            }
        }));
        this.subscriptions[state.url] = subscription;
        return subscription;
    };
    DeviceAuthGuard.decorators = [
        { type: Injectable, args: [{
                    providedIn: 'root'
                },] }
    ];
    /** @nocollapse */
    DeviceAuthGuard.ctorParameters = function () { return [
        { type: CurrentDeviceService },
        { type: Router },
        { type: ActivatedRoute },
        { type: undefined, decorators: [{ type: Inject, args: [DEVICE_AUTH_GUARD_CONFIG,] }] }
    ]; };
    /** @nocollapse */ DeviceAuthGuard.ɵprov = ɵɵdefineInjectable({ factory: function DeviceAuthGuard_Factory() { return new DeviceAuthGuard(ɵɵinject(CurrentDeviceService), ɵɵinject(Router), ɵɵinject(ActivatedRoute), ɵɵinject(DEVICE_AUTH_GUARD_CONFIG)); }, token: DeviceAuthGuard, providedIn: "root" });
    return DeviceAuthGuard;
}());
if (false) {
    /**
     * @type {?}
     * @private
     */
    DeviceAuthGuard.prototype.subscriptions;
    /**
     * @type {?}
     * @private
     */
    DeviceAuthGuard.prototype.tapService;
    /**
     * @type {?}
     * @private
     */
    DeviceAuthGuard.prototype.router;
    /**
     * @type {?}
     * @private
     */
    DeviceAuthGuard.prototype.route;
    /** @type {?} */
    DeviceAuthGuard.prototype.config;
}

/**
 * @fileoverview added by tsickle
 * Generated from: lib/tap-auth.module.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
var TapAuthModule = /** @class */ (function () {
    function TapAuthModule() {
    }
    TapAuthModule.decorators = [
        { type: NgModule, args: [{
                    declarations: [
                        DeviceLoginComponent,
                        ChangePasswordComponent
                    ],
                    exports: [
                        DeviceLoginComponent,
                        ChangePasswordComponent,
                    ],
                    providers: [
                        DeviceAuthGuard
                    ],
                    entryComponents: [
                        DeviceLoginComponent,
                        ChangePasswordComponent
                    ],
                    imports: [
                        LibCommonModule,
                        FormsModule,
                        ReactiveFormsModule,
                        TaskManagerUiModule
                    ]
                },] }
    ];
    return TapAuthModule;
}());

/**
 * @fileoverview added by tsickle
 * Generated from: public_api.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */

/**
 * @fileoverview added by tsickle
 * Generated from: iotize-ionic-auth.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */

export { ChangePasswordComponent, DEVICE_AUTH_GUARD_CONFIG, DeviceAuthGuard, DeviceLoginComponent, TapAuthModule, TapAuthService };
//# sourceMappingURL=iotize-ionic-auth.js.map
