"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _LocalUtilities_productNameSeed;
Object.defineProperty(exports, "__esModule", { value: true });
const os = require("os");
const crypto = require("crypto");
const NodeStorage_1 = require("./NodeStorage");
const Log_1 = require("../core/Log");
class LocalUtilities {
    constructor() {
        _LocalUtilities_productNameSeed.set(this, "mctools");
    }
    get isWindows() {
        return os.platform() === "win32";
    }
    get productNameSeed() {
        return __classPrivateFieldGet(this, _LocalUtilities_productNameSeed, "f");
    }
    setProductNameSeed(newSeed) {
        __classPrivateFieldSet(this, _LocalUtilities_productNameSeed, newSeed, "f");
    }
    get userDataPath() {
        return os.homedir();
    }
    get localAppDataPath() {
        if (this.isWindows) {
            return (this.userDataPath +
                NodeStorage_1.default.platformFolderDelimiter +
                "AppData" +
                NodeStorage_1.default.platformFolderDelimiter +
                "Local" +
                NodeStorage_1.default.platformFolderDelimiter);
        }
        else {
            return this.userDataPath;
        }
    }
    get localServerLogPath() {
        if (this.isWindows) {
            return (this.userDataPath +
                NodeStorage_1.default.platformFolderDelimiter +
                "AppData" +
                NodeStorage_1.default.platformFolderDelimiter +
                "Roaming" +
                NodeStorage_1.default.platformFolderDelimiter +
                "logs" +
                NodeStorage_1.default.platformFolderDelimiter);
        }
        else {
            return "." + NodeStorage_1.default.platformFolderDelimiter;
        }
    }
    get minecraftPath() {
        return (this.localAppDataPath +
            "Packages" +
            NodeStorage_1.default.platformFolderDelimiter +
            "Microsoft.MinecraftUWP_8wekyb3d8bbwe" +
            NodeStorage_1.default.platformFolderDelimiter +
            "LocalState" +
            NodeStorage_1.default.platformFolderDelimiter +
            "games" +
            NodeStorage_1.default.platformFolderDelimiter +
            "com.mojang" +
            NodeStorage_1.default.platformFolderDelimiter);
    }
    get minecraftPreviewPath() {
        return (this.localAppDataPath +
            "Packages" +
            NodeStorage_1.default.platformFolderDelimiter +
            "Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe" +
            NodeStorage_1.default.platformFolderDelimiter +
            "LocalState" +
            NodeStorage_1.default.platformFolderDelimiter +
            "games" +
            NodeStorage_1.default.platformFolderDelimiter +
            "com.mojang" +
            NodeStorage_1.default.platformFolderDelimiter);
    }
    get testWorkingPath() {
        let path = this.localAppDataPath;
        path =
            NodeStorage_1.default.ensureEndsWithDelimiter(path) +
                (this.isWindows ? "" : ".") +
                __classPrivateFieldGet(this, _LocalUtilities_productNameSeed, "f") +
                "_test" +
                NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get cliWorkingPath() {
        let path = this.localAppDataPath;
        path =
            NodeStorage_1.default.ensureEndsWithDelimiter(path) +
                (this.isWindows ? "" : ".") +
                __classPrivateFieldGet(this, _LocalUtilities_productNameSeed, "f") +
                "_cli" +
                NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get serverWorkingPath() {
        let path = this.localAppDataPath;
        path =
            NodeStorage_1.default.ensureEndsWithDelimiter(path) +
                (this.isWindows ? "" : ".") +
                __classPrivateFieldGet(this, _LocalUtilities_productNameSeed, "f") +
                "_server" +
                NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get worldsWorkingPath() {
        let path = this.localAppDataPath;
        path =
            NodeStorage_1.default.ensureEndsWithDelimiter(path) +
                (this.isWindows ? "" : ".") +
                __classPrivateFieldGet(this, _LocalUtilities_productNameSeed, "f") +
                "_worlds" +
                NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get serversPath() {
        let path = this.serverWorkingPath;
        path = NodeStorage_1.default.ensureEndsWithDelimiter(path) + "servers" + NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get sourceServersPath() {
        let path = this.serverWorkingPath;
        path = NodeStorage_1.default.ensureEndsWithDelimiter(path) + "serverSources" + NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get packCachePath() {
        let path = this.serverWorkingPath;
        path = NodeStorage_1.default.ensureEndsWithDelimiter(path) + "packCache" + NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    get envPrefsPath() {
        let path = this.serverWorkingPath;
        path = NodeStorage_1.default.ensureEndsWithDelimiter(path) + "envprefs" + NodeStorage_1.default.platformFolderDelimiter;
        return path;
    }
    generateCryptoRandomNumber(toVal) {
        Log_1.default.assert(toVal === 2 || toVal === 4 || toVal === 8 || toVal === 16 || toVal === 32 || toVal === 64 || toVal === 256);
        return new Uint32Array(crypto.randomBytes(1))[0] % toVal;
    }
    validateFolderPath(path) {
        // banned character combos
        if (path.indexOf("..") >= 0 || path.indexOf("\\\\") >= 0 || path.indexOf("//") >= 0) {
            throw new Error("Unsupported path combinations: " + path);
        }
        if (path.lastIndexOf(":") >= 3) {
            throw new Error("Unsupported drive location: " + path);
        }
        const count = this.countChar(path, "\\") + this.countChar(path, "/");
        if (count < 3) {
            throw new Error("Unsupported base path: " + path);
        }
    }
    countChar(source, find) {
        let count = 0;
        let index = source.indexOf(find);
        while (index >= 0) {
            count++;
            index = source.indexOf(find, index + find.length);
        }
        return count;
    }
    ensureStartsWithSlash(pathSegment) {
        if (!pathSegment.startsWith("/")) {
            pathSegment = "/" + pathSegment;
        }
        return pathSegment;
    }
    ensureEndsWithSlash(pathSegment) {
        if (!pathSegment.endsWith("/")) {
            pathSegment += "/";
        }
        return pathSegment;
    }
    ensureStartsWithBackSlash(pathSegment) {
        if (!pathSegment.startsWith("\\")) {
            pathSegment = "\\" + pathSegment;
        }
        return pathSegment;
    }
    ensureEndsWithBackSlash(pathSegment) {
        if (!pathSegment.endsWith("\\")) {
            pathSegment += "\\";
        }
        return pathSegment;
    }
    getFullPath(path) {
        let fullPath = __dirname;
        const lastSlash = Math.max(fullPath.lastIndexOf("\\", fullPath.length - 2), fullPath.lastIndexOf("/", fullPath.length - 2));
        if (lastSlash >= 0) {
            fullPath = fullPath.substring(0, lastSlash + 1);
        }
        if (this.isWindows) {
            fullPath += path.replace(/\//g, "\\");
        }
        else {
            fullPath += path.replace(/\\/g, NodeStorage_1.default.platformFolderDelimiter);
        }
        return fullPath;
    }
    async createStorage(path) {
        const fullPath = this.getFullPath(path);
        return new NodeStorage_1.default(fullPath, "");
    }
    async readJsonFile(path) {
        const fs = require("fs");
        const fullPath = this.getFullPath(path);
        const rawData = fs.readFileSync(fullPath);
        if (!rawData) {
            return null;
        }
        const jsonData = JSON.parse(rawData);
        return jsonData;
    }
    async processConversion(conversionSettings) {
        console.log("Converting!");
        return true;
    }
}
exports.default = LocalUtilities;
_LocalUtilities_productNameSeed = new WeakMap();

//# sourceMappingURL=../maps/local/LocalUtilities.js.map
