<commit_before>import { SlotsService } from 'app/shared/services/slots.service';
import { Component, ViewChild, OnInit, Output } from '@angular/core';
import { Http } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import { TranslateService } from '@ngx-translate/core';

import { ConfigService } from './../shared/services/config.service';
import { ArmTryService } from './../shared/services/arm-try.service';
import { CacheService } from './../shared/services/cache.service';
import { AuthzService } from './../shared/services/authz.service';
import { LanguageService } from './../shared/services/language.service';
import { ArmService } from './../shared/services/arm.service';
import { FunctionApp } from './../shared/function-app';
import { Site } from './../shared/models/arm/site';
import { ArmObj } from './../shared/models/arm/arm-obj';
import { ErrorIds } from './../shared/models/error-ids';
import { FunctionsService } from '../shared/services/functions.service';
import { BroadcastService } from '../shared/services/broadcast.service';
import { UserService } from '../shared/services/user.service';
import { BroadcastEvent } from '../shared/models/broadcast-event'
import { FunctionTemplate } from '../shared/models/function-template';
import { FunctionInfo } from '../shared/models/function-info';
import { BindingManager } from '../shared/models/binding-manager';
import { ErrorEvent, ErrorType } from '../shared/models/error-event';
import { GlobalStateService } from '../shared/services/global-state.service';
import { UIResource } from '../shared/models/ui-resource';
import { BusyStateComponent } from '../busy-state/busy-state.component';
import { PortalResources } from '../shared/models/portal-resources';
import { AiService } from '../shared/services/ai.service';

@Component({
    selector: 'try-landing',
    templateUrl: './try-landing.component.html',
    styleUrls: ['./try-landing.component.scss']
})
export class TryLandingComponent implements OnInit {
    @ViewChild(BusyStateComponent) busyState: BusyStateComponent;
    @Output() tryFunctionApp: Subject<FunctionApp>;
    public functionsInfo: FunctionInfo[] = new Array();
    bc: BindingManager = new BindingManager();
    loginOptions = false;
    selectedFunction: string;
    selectedLanguage: string;

    private _functionApp: FunctionApp;

    constructor(
        private _httpService: Http,
        private _functionsService: FunctionsService,
        private _broadcastService: BroadcastService,
        private _globalStateService: GlobalStateService,
        private _userService: UserService,
        private _translateService: TranslateService,
        private _aiService: AiService,
        private _armService: ArmService,
        private _cacheService: CacheService,
        private _languageService: LanguageService,
        private _authZService: AuthzService,
        private _configService: ConfigService,
        private _slotsService: SlotsService) {
        this.tryFunctionApp = new Subject<FunctionApp>();
    }

    ngOnInit() {
        // Disabling this temporarily. Somehow ngOnInit gets called twice on refresh
        // possibly related to https://github.com/angular/angular/issues/6782
        // and strangely the clearbusystate doesnt get called.
        // this.setBusyState();
        this.selectedFunction = this._functionsService.selectedFunction || 'HttpTrigger';
        this.selectedLanguage = this._functionsService.selectedLanguage || 'CSharp';
        this._functionsService.getTemplates().subscribe((templates) => {
            if (this._globalStateService.TryAppServiceToken) {
                const selectedTemplate: FunctionTemplate = templates.find((t) => {
                    return t.id === this.selectedFunction + '-' + this.selectedLanguage;
                });

                if (selectedTemplate) {
                    this.setBusyState();
                    this._functionsService.createTrialResource(selectedTemplate,
                        this._functionsService.selectedProvider, this._functionsService.selectedFunctionName)
                        .subscribe((resource) => {
                            this.clearBusyState();
                            this.createFunctioninResource(resource, selectedTemplate, this._functionsService.selectedFunctionName);
                        },
                        error => {
                            if (error.status === 400) {
                                // If there is already a free resource assigned ,
                                // we'll get a HTTP 400 ..so lets get it.
                                this._functionsService.getTrialResource(this._functionsService.selectedProvider)
                                    .subscribe((resource) => {
                                        this.createFunctioninResource(resource, selectedTemplate, this._functionsService.selectedFunctionName);
                                    });
                            } else {
                                this.clearBusyState();
                            }
                        });
                }
            }
        });

        const result = {
            name: this._translateService.instant(PortalResources.sideBar_newFunction),
            href: null,
            config: null,
            script_href: null,
            template_id: null,
            clientOnly: true,
            isDeleted: false,
            secrets_file_href: null,
            test_data: null,
            script_root_path_href: null,
            config_href: null,
            functionApp: null
        };

        this.functionsInfo.push(result);
    }

    onFunctionClicked(selectedFunction: string) {
        if (!this._broadcastService.getDirtyState('function_disabled')) {
            this.selectedFunction = selectedFunction;
        }
    }

    onLanguageClicked(selectedLanguage: string) {
        if (!this._broadcastService.getDirtyState('function_disabled')) {
            this.selectedLanguage = selectedLanguage;
        }
    }

    handleLoginClick(provider: string) {
        this._functionsService.getTemplates().subscribe((templates) => {
            const selectedTemplate: FunctionTemplate = templates.find((t) => {
                return t.id === this.selectedFunction + '-' + this.selectedLanguage;
            });
            if (provider === '') {
                // clicked on "Create this Function" button
                this.loginOptions = true;
            }
            else
                if (selectedTemplate) {
                    try {
                        const functionName = BindingManager.getFunctionName(selectedTemplate.metadata.defaultFunctionName, this.functionsInfo);
                        this.bc.setDefaultValues(selectedTemplate.function.bindings, this._globalStateService.DefaultStorageAccount);

                        this.setBusyState();
                        // login
                        // get trial account
                        this._functionsService.createTrialResource(selectedTemplate, provider, functionName)
                            .subscribe((resource) => {
                                this.clearBusyState();
                                this.createFunctioninResource(resource, selectedTemplate, functionName);
                            }, error => {
                                if (error.status === 401 || error.status === 403) {
                                    // show login options
                                    const headerObject = JSON.parse(JSON.stringify(error.headers))['LoginUrl'];
                                    if (provider !== '' && headerObject && headerObject[0]) {
                                        (<any>window).location = headerObject[0];<commit_msg>Fix LoginUrl access using Headers collection<commit_after>import { SlotsService } from 'app/shared/services/slots.service';
import { Component, ViewChild, OnInit, Output } from '@angular/core';
import { Http } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import { TranslateService } from '@ngx-translate/core';

import { ConfigService } from './../shared/services/config.service';
import { ArmTryService } from './../shared/services/arm-try.service';
import { CacheService } from './../shared/services/cache.service';
import { AuthzService } from './../shared/services/authz.service';
import { LanguageService } from './../shared/services/language.service';
import { ArmService } from './../shared/services/arm.service';
import { FunctionApp } from './../shared/function-app';
import { Site } from './../shared/models/arm/site';
import { ArmObj } from './../shared/models/arm/arm-obj';
import { ErrorIds } from './../shared/models/error-ids';
import { FunctionsService } from '../shared/services/functions.service';
import { BroadcastService } from '../shared/services/broadcast.service';
import { UserService } from '../shared/services/user.service';
import { BroadcastEvent } from '../shared/models/broadcast-event'
import { FunctionTemplate } from '../shared/models/function-template';
import { FunctionInfo } from '../shared/models/function-info';
import { BindingManager } from '../shared/models/binding-manager';
import { ErrorEvent, ErrorType } from '../shared/models/error-event';
import { GlobalStateService } from '../shared/services/global-state.service';
import { UIResource } from '../shared/models/ui-resource';
import { BusyStateComponent } from '../busy-state/busy-state.component';
import { PortalResources } from '../shared/models/portal-resources';
import { AiService } from '../shared/services/ai.service';

@Component({
    selector: 'try-landing',
    templateUrl: './try-landing.component.html',
    styleUrls: ['./try-landing.component.scss']
})
export class TryLandingComponent implements OnInit {
    @ViewChild(BusyStateComponent) busyState: BusyStateComponent;
    @Output() tryFunctionApp: Subject<FunctionApp>;
    public functionsInfo: FunctionInfo[] = new Array();
    bc: BindingManager = new BindingManager();
    loginOptions = false;
    selectedFunction: string;
    selectedLanguage: string;

    private _functionApp: FunctionApp;

    constructor(
        private _httpService: Http,
        private _functionsService: FunctionsService,
        private _broadcastService: BroadcastService,
        private _globalStateService: GlobalStateService,
        private _userService: UserService,
        private _translateService: TranslateService,
        private _aiService: AiService,
        private _armService: ArmService,
        private _cacheService: CacheService,
        private _languageService: LanguageService,
        private _authZService: AuthzService,
        private _configService: ConfigService,
        private _slotsService: SlotsService) {
        this.tryFunctionApp = new Subject<FunctionApp>();
    }

    ngOnInit() {
        // Disabling this temporarily. Somehow ngOnInit gets called twice on refresh
        // possibly related to https://github.com/angular/angular/issues/6782
        // and strangely the clearbusystate doesnt get called.
        // this.setBusyState();
        this.selectedFunction = this._functionsService.selectedFunction || 'HttpTrigger';
        this.selectedLanguage = this._functionsService.selectedLanguage || 'CSharp';
        this._functionsService.getTemplates().subscribe((templates) => {
            if (this._globalStateService.TryAppServiceToken) {
                const selectedTemplate: FunctionTemplate = templates.find((t) => {
                    return t.id === this.selectedFunction + '-' + this.selectedLanguage;
                });

                if (selectedTemplate) {
                    this.setBusyState();
                    this._functionsService.createTrialResource(selectedTemplate,
                        this._functionsService.selectedProvider, this._functionsService.selectedFunctionName)
                        .subscribe((resource) => {
                            this.clearBusyState();
                            this.createFunctioninResource(resource, selectedTemplate, this._functionsService.selectedFunctionName);
                        },
                        error => {
                            if (error.status === 400) {
                                // If there is already a free resource assigned ,
                                // we'll get a HTTP 400 ..so lets get it.
                                this._functionsService.getTrialResource(this._functionsService.selectedProvider)
                                    .subscribe((resource) => {
                                        this.createFunctioninResource(resource, selectedTemplate, this._functionsService.selectedFunctionName);
                                    });
                            } else {
                                this.clearBusyState();
                            }
                        });
                }
            }
        });

        const result = {
            name: this._translateService.instant(PortalResources.sideBar_newFunction),
            href: null,
            config: null,
            script_href: null,
            template_id: null,
            clientOnly: true,
            isDeleted: false,
            secrets_file_href: null,
            test_data: null,
            script_root_path_href: null,
            config_href: null,
            functionApp: null
        };

        this.functionsInfo.push(result);
    }

    onFunctionClicked(selectedFunction: string) {
        if (!this._broadcastService.getDirtyState('function_disabled')) {
            this.selectedFunction = selectedFunction;
        }
    }

    onLanguageClicked(selectedLanguage: string) {
        if (!this._broadcastService.getDirtyState('function_disabled')) {
            this.selectedLanguage = selectedLanguage;
        }
    }

    handleLoginClick(provider: string) {
        this._functionsService.getTemplates().subscribe((templates) => {
            const selectedTemplate: FunctionTemplate = templates.find((t) => {
                return t.id === this.selectedFunction + '-' + this.selectedLanguage;
            });
            if (provider === '') {
                // clicked on "Create this Function" button
                this.loginOptions = true;
            }
            else
                if (selectedTemplate) {
                    try {
                        const functionName = BindingManager.getFunctionName(selectedTemplate.metadata.defaultFunctionName, this.functionsInfo);
                        this.bc.setDefaultValues(selectedTemplate.function.bindings, this._globalStateService.DefaultStorageAccount);

                        this.setBusyState();
                        // login
                        // get trial account
                        this._functionsService.createTrialResource(selectedTemplate, provider, functionName)
                            .subscribe((resource) => {
                                this.clearBusyState();
                                this.createFunctioninResource(resource, selectedTemplate, functionName);
                            }, (error: Response) => {
                                if (error.status === 401 || error.status === 403) {
                                    // show login options
                                    const headerObject = error.headers['LoginUrl'];
                                    if (provider !== '' && headerObject && headerObject[0]) {
                                        (<any>window).location = headerObject[0];
{'fraction_non_alphanumeric': 0.10714972747675537, 'fraction_numerical': 0.0023084321898044244, 'mean_word_length': 2.03956343792633, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2498998', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3532, 'n_words': 755}