<commit_before>import ActiveSymbols from './activeSymbols';
import config from '../../common/const';
import { getObjectValue } from '../../../common/utils/tools';
import { getTokenList } from '../../../common/utils/storageManager';

const noop = () => {};

let parsedAssetIndex;

const parseAssetIndex = assetIndex => {
    const parsed = {};

    assetIndex.forEach(symbol => {
        parsed[symbol[0].toLowerCase()] = {};

        symbol[2].forEach(category => {
            [, , parsed[symbol[0].toLowerCase()][category[0].toLowerCase()]] = category;
        });
    });
    return parsed;
};

const getAllowedConditionsOrCategoriesForSymbol = symbol => {
    let conditions = [];
    const categories = [];
    const index = parsedAssetIndex[symbol.toLowerCase()];
    if (index) {
        Object.keys(config.conditionsCategory).forEach(conditionName => {
            if (conditionName in index) {
                conditions = conditions.concat(config.conditionsCategory[conditionName]);
                categories.push(conditionName);
            }
        });
    }
    return { conditions, categories };
};

const getCategoryForCondition = condition =>
    Object.keys(config.conditionsCategory).find(
        category => config.conditionsCategory[category].indexOf(condition.toLowerCase()) >= 0
    );

export default class _Symbol {
    constructor(api) {
        this.api = api;
        this.initPromise = new Promise(resolve => {
            const getActiveSymbolsPromise = () => {
                this.api.getActiveSymbolsBrief().then(r => {
                    this.activeSymbols = new ActiveSymbols(r.active_symbols);
                    this.api.getAssetIndex().then(r2 => {
                        parsedAssetIndex = parseAssetIndex(r2.asset_index);
                        resolve();
                    }, noop);
                }, noop);
            };
            // Authorize when possible for accurate offered symbols, assetindex
            const getAuthorisePromise = () => {
                const tokenList = getTokenList();
                if (tokenList.length) {
                    return this.api.authorize(tokenList[0].token);
                }
                return new Promise(r => r());
            };
            getAuthorisePromise()
                .then(() => getActiveSymbolsPromise(), noop)
                .catch(() => getActiveSymbolsPromise(), noop);
        });
    }
    /* eslint-disable class-methods-use-this */
    getLimitation(symbol, condition) {
        const category = getCategoryForCondition(condition);
        return {
            minDuration: parsedAssetIndex[symbol.toLowerCase()][category],<commit_msg>Remove tokens when invalid<commit_after>import ActiveSymbols from './activeSymbols';
import config from '../../common/const';
import { getObjectValue } from '../../../common/utils/tools';
import { getTokenList, removeAllTokens } from '../../../common/utils/storageManager';

const noop = () => {};

let parsedAssetIndex;

const parseAssetIndex = assetIndex => {
    const parsed = {};

    assetIndex.forEach(symbol => {
        parsed[symbol[0].toLowerCase()] = {};

        symbol[2].forEach(category => {
            [, , parsed[symbol[0].toLowerCase()][category[0].toLowerCase()]] = category;
        });
    });
    return parsed;
};

const getAllowedConditionsOrCategoriesForSymbol = symbol => {
    let conditions = [];
    const categories = [];
    const index = parsedAssetIndex[symbol.toLowerCase()];
    if (index) {
        Object.keys(config.conditionsCategory).forEach(conditionName => {
            if (conditionName in index) {
                conditions = conditions.concat(config.conditionsCategory[conditionName]);
                categories.push(conditionName);
            }
        });
    }
    return { conditions, categories };
};

const getCategoryForCondition = condition =>
    Object.keys(config.conditionsCategory).find(
        category => config.conditionsCategory[category].indexOf(condition.toLowerCase()) >= 0
    );

export default class _Symbol {
    constructor(api) {
        this.api = api;
        this.initPromise = new Promise(resolve => {
            const getActiveSymbolsLogic = () => {
                this.api.getActiveSymbolsBrief().then(r => {
                    this.activeSymbols = new ActiveSymbols(r.active_symbols);
                    this.api.getAssetIndex().then(r2 => {
                        parsedAssetIndex = parseAssetIndex(r2.asset_index);
                        resolve();
                    }, noop);
                }, noop);
            };
            // Authorize the WS connection when possible for accurate offered Symbols & AssetIndex
            const tokenList = getTokenList();
            if (tokenList.length) {
                this.api
                    .authorize(tokenList[0].token)
                    .then(getActiveSymbolsLogic())
                    .catch(() => {
                        removeAllTokens();
                        getActiveSymbolsLogic();
                    });
            } else {
                getActiveSymbolsLogic();
            }
        });
    }
    /* eslint-disable class-methods-use-this */
    getLimitation(symbol, condition) {
        const category = getCategoryForCondition(condition);
        return {
            minDuration: parsedAssetIndex[symbol.toLowerCase()][category],
{'fraction_non_alphanumeric': 0.12777053455019557, 'fraction_numerical': 0.002980070776680946, 'mean_word_length': 2.056346044393853, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, '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': '3208431', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1329, 'n_words': 243}