<commit_before>﻿import ConverterFactory = require('./converter/ConverterFactory');
import Crumb = require('./config/Crumb');
import NavigationDataManager = require('./NavigationDataManager');
import Route = require('./routing/Route');
import State = require('./config/State');
import StateRouter = require('./StateRouter');

class StateHandler {
    static getNavigationLink(router: StateRouter, converterFactory: ConverterFactory, state: State, navigationData: any, crumbTrail: string[]): string {
        var { data, arrayData } = NavigationDataManager.formatData(converterFactory, state, navigationData, crumbTrail);
        var routeInfo = router.getRoute(state, data, arrayData);
        if (routeInfo.route == null)
            return null;
        var query: string[] = [];
        for (var key in data) {
            if (!routeInfo.data[key]) {
                var arr = arrayData[key];
                var encodedKey = state.urlEncode(state, null, key, true);
                if (!arr) {
                    query.push(encodedKey + '=' + state.urlEncode(state, key, data[key], true));
                } else {
                    for(var i = 0; i < arr.length; i++)
                        query.push(encodedKey + '=' + state.urlEncode(state, key, arr[i], true));
                }
            }
        }
        if (query.length > 0)
            routeInfo.route += '?' + query.join('&');
        return routeInfo.route;
    }

    static parseNavigationLink(router: StateRouter, converterFactory: ConverterFactory, url: string, fromRoute?: Route): { state: State, data: any } {
        var queryIndex = url.indexOf('?');
        var path = queryIndex < 0 ? url : url.substring(0, queryIndex);
        var query = queryIndex >= 0 ? url.substring(queryIndex + 1) : null;
        var match = router.getData(path, fromRoute);
        if (!match)
            return null;
        try{
            var { state, data, separableData, route } = match;
            var navigationData = this.getNavigationData(router, converterFactory, query, state, data || {}, separableData);
        } catch(e) {
        }
        return navigationData || this.parseNavigationLink(router, converterFactory, url, route);        
    }

    private static getNavigationData(router: StateRouter, converterFactory: ConverterFactory, query: string, state: State, data: any, separableData: any): { state: State, data: any } {
        if (query) {
            var params = query.split('&');
            for (var i = 0; i < params.length; i++) {
                var param = params[i].split('=');
                var key = state.urlDecode(state, null, param[0], true);
                var val = state.urlDecode(state, key, param[1], true);
                separableData[key] = true;
                var arr = data[key];
                if (!arr) {
                    data[key] = val;
                } else {
                    if (typeof arr === 'string')<commit_msg>Moved destructure outside try catch<commit_after>﻿import ConverterFactory = require('./converter/ConverterFactory');
import Crumb = require('./config/Crumb');
import NavigationDataManager = require('./NavigationDataManager');
import Route = require('./routing/Route');
import State = require('./config/State');
import StateRouter = require('./StateRouter');

class StateHandler {
    static getNavigationLink(router: StateRouter, converterFactory: ConverterFactory, state: State, navigationData: any, crumbTrail: string[]): string {
        var { data, arrayData } = NavigationDataManager.formatData(converterFactory, state, navigationData, crumbTrail);
        var routeInfo = router.getRoute(state, data, arrayData);
        if (routeInfo.route == null)
            return null;
        var query: string[] = [];
        for (var key in data) {
            if (!routeInfo.data[key]) {
                var arr = arrayData[key];
                var encodedKey = state.urlEncode(state, null, key, true);
                if (!arr) {
                    query.push(encodedKey + '=' + state.urlEncode(state, key, data[key], true));
                } else {
                    for(var i = 0; i < arr.length; i++)
                        query.push(encodedKey + '=' + state.urlEncode(state, key, arr[i], true));
                }
            }
        }
        if (query.length > 0)
            routeInfo.route += '?' + query.join('&');
        return routeInfo.route;
    }

    static parseNavigationLink(router: StateRouter, converterFactory: ConverterFactory, url: string, fromRoute?: Route): { state: State, data: any } {
        var queryIndex = url.indexOf('?');
        var path = queryIndex < 0 ? url : url.substring(0, queryIndex);
        var query = queryIndex >= 0 ? url.substring(queryIndex + 1) : null;
        var match = router.getData(path, fromRoute);
        if (!match)
            return null;
        var { state, data, separableData, route } = match;
        try{
            var navigationData = this.getNavigationData(router, converterFactory, query, state, data || {}, separableData);
        } catch(e) {
        }
        return navigationData || this.parseNavigationLink(router, converterFactory, url, route);        
    }

    private static getNavigationData(router: StateRouter, converterFactory: ConverterFactory, query: string, state: State, data: any, separableData: any): { state: State, data: any } {
        if (query) {
            var params = query.split('&');
            for (var i = 0; i < params.length; i++) {
                var param = params[i].split('=');
                var key = state.urlDecode(state, null, param[0], true);
                var val = state.urlDecode(state, key, param[1], true);
                separableData[key] = true;
                var arr = data[key];
                if (!arr) {
                    data[key] = val;
                } else {
                    if (typeof arr === 'string')
{'fraction_non_alphanumeric': 0.13024357239512854, 'fraction_numerical': 0.003044654939106901, 'mean_word_length': 2.2364532019704435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792142', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1534, 'n_words': 429}