"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular/core");
const http_1 = require("@angular/http");
const platform_browser_1 = require("@angular/platform-browser");
const Observable_1 = require("rxjs/Observable");
require("rxjs/add/observable/interval");
require("rxjs/add/operator/map");
require("rxjs/add/operator/switchMap");
require("rxjs/add/operator/take");
require("rxjs/add/operator/takeUntil");
require("../util/takeUntilDestroyed");
const env_1 = require("../util/env");
const memorizingSubject_1 = require("../util/memorizingSubject");
/**
 * The Launch Dialog prompts the user to log in, and connect to Interactive
 * for them. It calls back with connection details, or undefined if the dialog
 * was dismissed beforehand.
 */
let LoginWalkthroughComponent = class LoginWalkthroughComponent {
    constructor(http, sanitizer) {
        this.http = http;
        this.sanitizer = sanitizer;
        /**
         * Emits once the user logs in.
         */
        this.loggedIn = new memorizingSubject_1.MemorizingSubject();
        /**
         * code is the shortcode to display to the user that they need to enter
         * on mixer.com/go in order to start login.
         */
        this.code = new memorizingSubject_1.MemorizingSubject();
        /**
         * codeUrl is the full Mixer URL the user should go to
         */
        this.codeUrl = this.code.map(code => this.sanitizer.bypassSecurityTrustUrl(`${env_1.mixerUrl()}/go?code=${code}`));
    }
    ngOnInit() {
        const sub = Observable_1.Observable.interval(1000)
            .startWith(null)
            .switchMap(() => this.http.get(env_1.apiUrl('login')))
            .takeUntilDestroyed(this)
            .map(res => res.json())
            .subscribe(res => {
            if (res.code) {
                this.code.next(res.code);
                return;
            }
            this.loggedIn.next(undefined);
            sub.unsubscribe();
        });
    }
    ngOnDestroy() {
        // noop
    }
};
__decorate([
    core_1.Output(),
    __metadata("design:type", Object)
], LoginWalkthroughComponent.prototype, "loggedIn", void 0);
LoginWalkthroughComponent = __decorate([
    core_1.Component({
        selector: 'editor-login-walkthrough',
        templateUrl: './login-walkthrough.component.html',
        styleUrls: ['../dialog.scss'],
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __metadata("design:paramtypes", [http_1.Http, platform_browser_1.DomSanitizer])
], LoginWalkthroughComponent);
exports.LoginWalkthroughComponent = LoginWalkthroughComponent;
