import { Injectable, ApplicationRef, ComponentFactoryResolver } from '@angular/core';
import { createBrowserAuthWrapper } from './browser-auth-wrapper';
import { parseToken } from './jwt-helper';
import { GHDAdalConfig } from '../../models/adal-config.model';
import { LoginComponent } from '../../components/login.component';
var AuthService = (function () {
    function AuthService(config, applicationRef, componentFactoryResolver) {
        this.applicationRef = applicationRef;
        this.componentFactoryResolver = componentFactoryResolver;
        this.authWrapper = createBrowserAuthWrapper(config);
    }
    AuthService.prototype.handleResponse = function () {
        this.authWrapper.handleResponse(window.location.hash);
    };
    AuthService.prototype.showLoginPrompt = function () {
        var _this = this;
        /*
          FIXME: Get ViewContainerRef from root component
          Inorder to attach component dynamically, we need ViewContainerRef.
          Right now there is no proper way to get ViewContainerRef from service.
        */
        var containerRef = this.applicationRef.components[0].instance.viewRef;
        if (containerRef) {
            // Use LoginComponent
            var componentFactory = this.componentFactoryResolver.resolveComponentFactory(LoginComponent);
            containerRef.clear();
            var componentRef = containerRef.createComponent(componentFactory);
            componentRef.instance.onClose().skip(1).subscribe(function () {
                _this.authWrapper.login();
            });
        }
        else {
            // Use standard Alert
            alert('You are not logged in, or your session has expired.  Please login again');
            this.authWrapper.login();
        }
    };
    AuthService.prototype.logout = function () {
        return this.authWrapper.logout();
    };
    AuthService.prototype.acquireTokenInBackground = function () {
        return this.authWrapper.acquireTokenInBackground();
    };
    Object.defineProperty(AuthService.prototype, "token", {
        get: function () {
            return this.authWrapper.token;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AuthService.prototype, "jwt", {
        get: function () {
            return this.token
                ? parseToken(this.token)
                : null;
        },
        enumerable: true,
        configurable: true
    });
    AuthService.decorators = [
        { type: Injectable },
    ];
    /** @nocollapse */
    AuthService.ctorParameters = function () { return [
        { type: GHDAdalConfig, },
        { type: ApplicationRef, },
        { type: ComponentFactoryResolver, },
    ]; };
    return AuthService;
}());
export { AuthService };
//# sourceMappingURL=auth.service.js.map