"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const IProjectItemData_1 = require("../app/IProjectItemData");
const Utilities_1 = require("../core/Utilities");
const Log_1 = require("../core/Log");
const Lang_1 = require("./Lang");
class LocManager {
    _project;
    _isLoaded;
    _languages = {};
    tokens = {};
    constructor(project) {
        this._project = project;
        this._isLoaded = false;
    }
    async getTokenValue(tokenName) {
        if (!this._isLoaded) {
            await this.load();
        }
        return this.getTokenValueOrDefault(tokenName);
    }
    getAllTokenKeys() {
        return Object.keys(this.tokens);
    }
    getAllLanguages() {
        let langs = [];
        for (const langKey in this._languages) {
            langs.push(...this._languages[langKey]);
        }
        return langs;
    }
    static canonicalizeLanguageKey(locKey) {
        return locKey.toLowerCase().trim();
    }
    getEnUsLang() {
        const primary = this._languages["en_us"];
        Log_1.default.assert(!!primary, "No en-us language found.");
        return primary;
    }
    getNonEnUsLangs() {
        let langs = [];
        for (const langKey in this._languages) {
            if (LocManager.canonicalizeLanguageKey(langKey) !== "en_us") {
                langs.push(...this._languages[langKey]);
            }
        }
        return langs;
    }
    getExpandedValue(tokenName) {
        if (!this._isLoaded) {
            return tokenName;
        }
        const tok = this.getToken(tokenName);
        if (!tok) {
            return tokenName;
        }
        return tok.value + " - (" + tokenName + ")";
    }
    getTokenValueOrDefault(tokenName) {
        if (!this._isLoaded) {
            return tokenName;
        }
        const tok = this.getToken(tokenName);
        if (!tok) {
            return tokenName;
        }
        return tok.value;
    }
    getToken(tokenName, locale, packContainer) {
        const tokenSets = this.tokens[tokenName];
        if (tokenSets) {
            if (!locale) {
                locale = "en_US";
            }
            const tokenSetByLocale = tokenSets[LocManager.canonicalizeLanguageKey(locale)];
            if (tokenSetByLocale) {
                if (packContainer) {
                    return tokenSetByLocale[packContainer];
                }
                // return the first available token
                for (const containerName in tokenSetByLocale) {
                    return tokenSetByLocale[containerName];
                }
            }
        }
        return undefined;
    }
    async load(force) {
        if (this._isLoaded && !force) {
            return;
        }
        await this._project.ensureLoadedProjectFolder();
        this.tokens = {};
        const itemsCopy = this._project.getItemsCopy();
        for (const pi of itemsCopy) {
            if (pi.itemType === IProjectItemData_1.ProjectItemType.lang) {
                if (!pi.isContentLoaded) {
                    await pi.loadContent();
                }
                if (pi.primaryFile) {
                    const lang = await Lang_1.default.ensureOnFile(pi.primaryFile);
                    if (lang) {
                        await lang.load();
                        if (lang.language && lang.containerName !== undefined && Utilities_1.default.isUsableAsObjectKey(lang.language)) {
                            const language = LocManager.canonicalizeLanguageKey(lang.language);
                            if (!this._languages[language]) {
                                this._languages[language] = [];
                            }
                            this._languages[language].push(lang);
                            for (const tokenName in lang.tokens) {
                                if (Utilities_1.default.isUsableAsObjectKey(tokenName)) {
                                    if (this.tokens[tokenName] === undefined) {
                                        this.tokens[tokenName] = {};
                                    }
                                    if (Utilities_1.default.isUsableAsObjectKey(lang.containerName)) {
                                        if (this.tokens[tokenName][language] === undefined) {
                                            this.tokens[tokenName][language] = {};
                                        }
                                        this.tokens[tokenName][language][lang.containerName] = lang.tokens[tokenName];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        this._isLoaded = true;
    }
}
exports.default = LocManager;

//# sourceMappingURL=../maps/minecraft/LocManager.js.map
