"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const IProjectItemData_1 = require("../app/IProjectItemData");
const Lang_1 = require("./Lang");
class LocManager {
    constructor(project) {
        this.tokens = {};
        this._project = project;
        this._isLoaded = false;
    }
    async getTokenValue(tokenName) {
        if (!this._isLoaded) {
            await this.load();
        }
        return this.getTokenValueOrDefault(tokenName);
    }
    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 loc = this.tokens[tokenName];
        if (loc) {
            if (!locale) {
                locale = "en_US";
            }
            const tokenSetByLocale = loc[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) {
                await pi.ensureFileStorage();
                if (pi.file) {
                    const lang = await Lang_1.default.ensureOnFile(pi.file);
                    if (lang) {
                        await lang.load();
                        if (lang.language && lang.containerName !== undefined) {
                            for (const tokenName in lang.tokens) {
                                if (this.tokens[tokenName] === undefined) {
                                    this.tokens[tokenName] = {};
                                }
                                if (this.tokens[tokenName][lang.language] === undefined) {
                                    this.tokens[tokenName][lang.language] = {};
                                }
                                this.tokens[tokenName][lang.language][lang.containerName] = lang.tokens[tokenName];
                            }
                        }
                    }
                }
            }
        }
        this._isLoaded = true;
    }
}
exports.default = LocManager;

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