"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Utilities_1 = require("../core/Utilities");
const Log_1 = require("../core/Log");
class MinecraftUtilities {
    static isReloadableSetOfChanges(differenceSet) {
        return differenceSet.hasFileOnlyOfExtension("js", "ts", "map") && !differenceSet.getHasDeletions();
    }
    static getAfterPackPath(path) {
        const hashIndex = path.indexOf("#");
        if (hashIndex >= 0) {
            path = path.substring(hashIndex + 1);
        }
        return path;
    }
    static replaceMinecraftPathTokens(tokenizePath) {
        if (tokenizePath.startsWith("<RPME>")) {
            tokenizePath += ".geo.json";
        }
        else if (tokenizePath.startsWith("<BPE>") ||
            tokenizePath.startsWith("<BPLE>") ||
            tokenizePath.startsWith("<BPSR>")) {
            tokenizePath += ".json";
        }
        else if (tokenizePath.startsWith("<RPE>")) {
            tokenizePath += ".entity.json";
        }
        else if (tokenizePath.startsWith("<RPAC>")) {
            tokenizePath += ".animation_controllers.json";
        }
        else if (tokenizePath.startsWith("<RPRC>")) {
            tokenizePath += ".render_controllers.json";
        }
        else if (tokenizePath.startsWith("<RPA>")) {
            tokenizePath += ".animation.json";
        }
        else if (tokenizePath.startsWith("<RPTE>")) {
            if (tokenizePath.indexOf(".") < 0) {
                tokenizePath += ".png";
            }
        }
        tokenizePath = tokenizePath.replace(/<RPTE>/gi, "/resource_pack/textures/entity/");
        tokenizePath = tokenizePath.replace(/<RPME>/gi, "/resource_pack/models/entity/");
        tokenizePath = tokenizePath.replace(/<RPAC>/gi, "/resource_pack/animation_controllers/");
        tokenizePath = tokenizePath.replace(/<RPRC>/gi, "/resource_pack/render_controllers/");
        tokenizePath = tokenizePath.replace(/<RPA>/gi, "/resource_pack/animations/");
        tokenizePath = tokenizePath.replace(/<RPE>/gi, "/resource_pack/entity/");
        tokenizePath = tokenizePath.replace(/<BPE>/gi, "/behavior_pack/entities/");
        tokenizePath = tokenizePath.replace(/<BPLE>/gi, "/behavior_pack/loot_tables/entities/");
        tokenizePath = tokenizePath.replace(/<BPSR>/gi, "/behavior_pack/spawn_rules/");
        return tokenizePath;
    }
    static makeNameScriptSafe(tokenName) {
        tokenName = tokenName.replace(/[^a-z0-9]/gi, "-").replace(/\s+/g, "-");
        while (tokenName.length > 1 && !Utilities_1.default.isAlpha(tokenName.substring(0, 1))) {
            tokenName = tokenName.substring(1);
        }
        if (!Utilities_1.default.isAlpha(tokenName.substring(0, 1))) {
            tokenName = "a" + tokenName;
        }
        return tokenName;
    }
    static makeNameFolderSafe(tokenName) {
        tokenName = Utilities_1.default.replaceAll(tokenName, " ", "-").toLowerCase();
        if (tokenName.length > 16) {
            tokenName = tokenName.substring(0, 16);
        }
        return tokenName;
    }
    static getVersionArrayFrom(ver) {
        if (typeof ver === "number") {
            return [ver];
        }
        if (typeof ver === "string") {
            let fvArr = ver.split(".");
            let fvArrInt = [];
            for (let i = 0; i < fvArr.length; i++) {
                try {
                    const num = parseInt(fvArr[i]);
                    if (isNaN(num)) {
                        return undefined;
                    }
                    fvArrInt.push(num);
                }
                catch (e) { }
            }
            return fvArrInt;
        }
        return ver;
    }
    static cleanUpScriptDescription(scriptDescription) {
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "/*", "");
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "*/\n", "");
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "*\n", "");
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "*/", "");
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "*", "");
        scriptDescription = Utilities_1.default.replaceAll(scriptDescription, "@beta", "");
        scriptDescription = scriptDescription.trim();
        return scriptDescription;
    }
    static getIdsAndVersions(packStr) {
        const refs = [];
        const packRefStrs = packStr.split(",");
        for (const packRefStr of packRefStrs) {
            const packRefStrParts = packRefStr.split("@");
            let wasAdded = false;
            if (packRefStrParts.length === 2) {
                const versionNumberParts = packRefStrParts[1].split(".");
                if (versionNumberParts.length === 3) {
                    const verNumber = [];
                    for (let i = 0; i < versionNumberParts.length; i++) {
                        let num = undefined;
                        try {
                            num = parseInt(versionNumberParts[i]);
                        }
                        catch (e) { }
                        if (num !== undefined) {
                            verNumber.push(num);
                        }
                    }
                    if (verNumber.length === 3 && this.isValidUuid(packRefStrParts[0])) {
                        refs.push({
                            uuid: packRefStrParts[0],
                            version: verNumber,
                        });
                        wasAdded = true;
                    }
                }
            }
            if (!wasAdded) {
                Log_1.default.fail("UUID/version is not in a good form (uuid@x.y.z) - is '" + packRefStr + "'");
            }
        }
        return refs;
    }
    static isValidUuid(uuid) {
        // sample uuid: 414a42cd-98d4-4e0d-8e3f-e34ab8a0c05e
        //              xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        if (uuid.length === 36) {
            const parts = uuid.split("-");
            if (parts.length === 5 &&
                parts[0].length === 8 &&
                parts[1].length === 4 &&
                parts[2].length === 4 &&
                parts[3].length === 4 &&
                parts[4].length === 12) {
                for (let i = 0; i < parts.length; i++) {
                    for (let j = 0; j < parts[i].length; j++) {
                        if (!Utilities_1.default.isAlphaNumeric(parts[i][j])) {
                            return false;
                        }
                    }
                }
            }
        }
        return false;
    }
    static fixupJson(json) {
        json = json.replace(/([, ;}\]])0b([, ;}\]])/gi, "$1false$2");
        json = json.replace(/([, ;}\]])1b([, ;}\]])/gi, "$1true$2");
        json = json.replace(/(\d)b([, ;\]])/gi, "$1$2");
        json = json.replace(/(\d)d([, ;\]])/gi, "$1$2");
        json = json.replace(/(\d)f([, ;\]])/gi, "$1$2");
        json = json.replace(/(\d)s([, ;\]])/gi, "$1$2");
        json = json.replace(/\[I;/gi, "[");
        json = json.replace(/…/gi, "");
        /*
            json = json.replace(/([, ;\]])none([, ;\]])/gi, "$1\"none\"$2");
            json = json.replace(/([, ;\]])south([, ;\]])/gi, "$1\"south\"$2");
            json = json.replace(/([, ;\]])north([, ;\]])/gi, "$1\"north\"$2");
            json = json.replace(/([, ;\]])east([, ;\]])/gi, "$1\"east\"$2");
            json = json.replace(/([, ;\]])west([, ;\]])/gi, "$1\"west\"$2");
            json = json.replace(/([, ;\]])side([, ;\]])/gi, "$1\"side\"$2");
    */
        return json;
    }
    static canonicalizeName(id) {
        let canonId = id.toLowerCase().trim();
        if (canonId.startsWith("minecraft:")) {
            canonId = canonId.substring(10, canonId.length);
        }
        return canonId;
    }
    static canonicalizeFullName(id) {
        let canonId = id.toLowerCase().trim();
        if (canonId.indexOf(":") < 0) {
            canonId = "minecraft:" + canonId;
        }
        return canonId;
    }
}
exports.default = MinecraftUtilities;

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