"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 _LocalEnvironment_data, _LocalEnvironment_isLoaded, _LocalEnvironment_prefsStorage, _LocalEnvironment_rootPrefFolder, _LocalEnvironment_configFile, _LocalEnvironment_worldContainerStorage, _LocalEnvironment_displayInfo, _LocalEnvironment_displayVerbose;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperationColors = exports.consoleText_bgGray = exports.consoleText_bgWhite = exports.consoleText_bgCyan = exports.consoleText_bgMagenta = exports.consoleText_bgBlue = exports.consoleText_bgYellow = exports.consoleText_bgGreen = exports.consoleText_bgRed = exports.consoleText_bgBlack = exports.consoleText_fgGray = exports.consoleText_fgWhite = exports.consoleText_fgCyan = exports.consoleText_fgMagenta = exports.consoleText_fgBlue = exports.consoleText_fgYellow = exports.consoleText_fgGreen = exports.consoleText_fgRed = exports.consoleText_fgBlack = exports.consoleText_hidden = exports.consoleText_reverse = exports.consoleText_blink = exports.consoleText_underscore = exports.consoleText_dim = exports.consoleText_bright = exports.consoleText_reset = void 0;
const LocalUtilities_1 = require("./LocalUtilities");
const NodeStorage_1 = require("./NodeStorage");
const fs = require("fs");
const Utilities_1 = require("../core/Utilities");
const crypto = require("crypto");
const worker_threads_1 = require("worker_threads");
const Log_1 = require("../core/Log");
const JsEslintInfoGenerator_1 = require("./JsEslintInfoGenerator");
const GeneratorRegistrations_1 = require("../info/GeneratorRegistrations");
exports.consoleText_reset = "\x1b[0m";
exports.consoleText_bright = "\x1b[1m";
exports.consoleText_dim = "\x1b[2m";
exports.consoleText_underscore = "\x1b[4m";
exports.consoleText_blink = "\x1b[5m";
exports.consoleText_reverse = "\x1b[7m";
exports.consoleText_hidden = "\x1b[8m";
exports.consoleText_fgBlack = "\x1b[30m";
exports.consoleText_fgRed = "\x1b[31m";
exports.consoleText_fgGreen = "\x1b[32m";
exports.consoleText_fgYellow = "\x1b[33m";
exports.consoleText_fgBlue = "\x1b[34m";
exports.consoleText_fgMagenta = "\x1b[35m";
exports.consoleText_fgCyan = "\x1b[36m";
exports.consoleText_fgWhite = "\x1b[37m";
exports.consoleText_fgGray = "\x1b[90m";
exports.consoleText_bgBlack = "\x1b[40m";
exports.consoleText_bgRed = "\x1b[41m";
exports.consoleText_bgGreen = "\x1b[42m";
exports.consoleText_bgYellow = "\x1b[43m";
exports.consoleText_bgBlue = "\x1b[44m";
exports.consoleText_bgMagenta = "\x1b[45m";
exports.consoleText_bgCyan = "\x1b[46m";
exports.consoleText_bgWhite = "\x1b[47m";
exports.consoleText_bgGray = "\x1b[100m";
exports.OperationColors = [exports.consoleText_fgGreen, exports.consoleText_fgCyan, exports.consoleText_fgBlue, exports.consoleText_fgMagenta];
class LocalEnvironment {
    get displayInfo() {
        return __classPrivateFieldGet(this, _LocalEnvironment_displayInfo, "f");
    }
    set displayInfo(newInfoValue) {
        __classPrivateFieldSet(this, _LocalEnvironment_displayInfo, newInfoValue, "f");
    }
    get displayVerbose() {
        return __classPrivateFieldGet(this, _LocalEnvironment_displayVerbose, "f");
    }
    set displayVerbose(newVerboseValue) {
        __classPrivateFieldSet(this, _LocalEnvironment_displayVerbose, newVerboseValue, "f");
    }
    get worldContainerStorage() {
        return __classPrivateFieldGet(this, _LocalEnvironment_worldContainerStorage, "f");
    }
    get worldContainerPath() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").worldContainerPath;
    }
    get serverHostPort() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverHostPort;
    }
    set serverHostPort(newPort) {
        if (newPort !== __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverHostPort) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverHostPort = newPort;
        }
    }
    get serverDomainName() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverDomainName;
    }
    set serverDomainName(newDomainName) {
        if (newDomainName !== __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverDomainName) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverDomainName = newDomainName;
        }
    }
    get serverTitle() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverTitle;
    }
    set serverTitle(newTitle) {
        if (newTitle !== __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverTitle) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverTitle = newTitle;
        }
    }
    get serverMessageOfTheDay() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverMessageOfTheDay;
    }
    set serverMessageOfTheDay(messageOfTheDay) {
        if (messageOfTheDay !== __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverMessageOfTheDay) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverMessageOfTheDay = messageOfTheDay;
        }
    }
    get displayReadOnlyPasscode() {
        return this._inmemDisplayReadOnlyPasscode; //this.#data.displayReadOnlyPasscode;
    }
    get displayReadOnlyPasscodeComplement() {
        return this._inmemDisplayReadOnlyPasscodeComplement; //this.#data.displayReadOnlyPasscodeComplement;
    }
    set displayReadOnlyPasscode(newPasscode) {
        if (newPasscode === undefined) {
            throw new Error();
        }
        newPasscode = newPasscode.toLowerCase();
        newPasscode = newPasscode.replace("-", "");
        if (newPasscode.length !== 8 || !Utilities_1.default.isAlphaNumeric(newPasscode)) {
            throw new Error("Improperly formatted display read-only passcode. Passcodes should be 8 alphanumeric characters.");
        }
        this._inmemDisplayReadOnlyPasscode = newPasscode;
        this._inmemDisplayReadOnlyPasscodeComplement = this.generateRandomPasscode();
    }
    get fullReadOnlyPasscode() {
        return this._inmemFullReadOnlyPasscode;
    }
    get fullReadOnlyPasscodeComplement() {
        return this._inmemFullReadOnlyPasscodeComplement;
    }
    set fullReadOnlyPasscode(newPasscode) {
        if (newPasscode === undefined) {
            throw new Error();
        }
        newPasscode = newPasscode.toLowerCase();
        newPasscode = newPasscode.replace("-", "");
        if (newPasscode.length !== 8 || !Utilities_1.default.isAlphaNumeric(newPasscode)) {
            throw new Error("Improperly formatted full read-only passcode. Passcodes should be 8 alphanumeric characters.");
        }
        this._inmemFullReadOnlyPasscode = newPasscode;
        this._inmemFullReadOnlyPasscodeComplement = this.generateRandomPasscode();
    }
    get updateStatePasscode() {
        return this._inmemUpdateStatePasscode;
    }
    get updateStatePasscodeComplement() {
        return this._inmemUpdateStatePasscodeComplement;
    }
    set updateStatePasscode(newPasscode) {
        if (newPasscode === undefined) {
            throw new Error();
        }
        newPasscode = newPasscode.toLowerCase();
        newPasscode = newPasscode.replace("-", "");
        if (newPasscode.length !== 8 || !Utilities_1.default.isAlphaNumeric(newPasscode)) {
            throw new Error("Improperly formatted update passcode. Passcodes should be 8 alphanumeric characters.");
        }
        this._inmemUpdateStatePasscode = newPasscode;
        this._inmemUpdateStatePasscodeComplement = this.generateRandomPasscode();
    }
    get adminPasscode() {
        return this._inmemAdminPasscode;
    }
    get adminPasscodeComplement() {
        return this._inmemAdminPasscodeComplement;
    }
    set adminPasscode(newPasscode) {
        if (newPasscode === undefined) {
            throw new Error();
        }
        newPasscode = newPasscode.toLowerCase();
        newPasscode = newPasscode.replace("-", "");
        if (newPasscode.length !== 8 || !Utilities_1.default.isAlphaNumeric(newPasscode)) {
            throw new Error("Improperly formatted admin passcode. Passcodes should be 8 alphanumeric characters.");
        }
        this._inmemAdminPasscode = newPasscode;
        this._inmemAdminPasscodeComplement = this.generateRandomPasscode();
    }
    get iAgreeToTheMinecraftEndUserLicenseAgreementAndPrivacyPolicyAtMinecraftDotNetSlashEula() {
        return __classPrivateFieldGet(this, _LocalEnvironment_data, "f").iAgreeToTheMinecraftEndUserLicenseAgreementAndPrivacyPolicyAtMinecraftDotNetSlashEula;
    }
    set iAgreeToTheMinecraftEndUserLicenseAgreementAndPrivacyPolicyAtMinecraftDotNetSlashEula(iAgreeValue) {
        __classPrivateFieldGet(this, _LocalEnvironment_data, "f").iAgreeToTheMinecraftEndUserLicenseAgreementAndPrivacyPolicyAtMinecraftDotNetSlashEula = iAgreeValue;
    }
    constructor(subscribeToLog) {
        _LocalEnvironment_data.set(this, void 0);
        _LocalEnvironment_isLoaded.set(this, false);
        _LocalEnvironment_prefsStorage.set(this, void 0);
        _LocalEnvironment_rootPrefFolder.set(this, void 0);
        _LocalEnvironment_configFile.set(this, void 0);
        _LocalEnvironment_worldContainerStorage.set(this, void 0);
        _LocalEnvironment_displayInfo.set(this, false);
        _LocalEnvironment_displayVerbose.set(this, false);
        __classPrivateFieldSet(this, _LocalEnvironment_data, {}, "f");
        this.load = this.load.bind(this);
        this.save = this.save.bind(this);
        this.utilities = new LocalUtilities_1.default();
        GeneratorRegistrations_1.default.projectGenerators.push(new JsEslintInfoGenerator_1.default());
        this.handleNewLogMessage = this.handleNewLogMessage.bind(this);
        if (subscribeToLog) {
            Log_1.default.onItemAdded.subscribe(this.handleNewLogMessage);
        }
        if (!fs.existsSync(this.utilities.serverWorkingPath)) {
            fs.mkdirSync(this.utilities.serverWorkingPath, { recursive: true });
        }
        __classPrivateFieldGet(this, _LocalEnvironment_data, "f").worldContainerPath = this.utilities.worldsWorkingPath;
        if (this.utilities.worldsWorkingPath && !fs.existsSync(this.utilities.worldsWorkingPath)) {
            fs.mkdirSync(this.utilities.worldsWorkingPath, { recursive: true });
        }
        __classPrivateFieldSet(this, _LocalEnvironment_worldContainerStorage, new NodeStorage_1.default(this.utilities.worldsWorkingPath, ""), "f");
        if (!fs.existsSync(this.utilities.cliWorkingPath)) {
            fs.mkdirSync(this.utilities.cliWorkingPath, { recursive: true });
        }
        if (!fs.existsSync(this.utilities.envPrefsPath)) {
            fs.mkdirSync(this.utilities.envPrefsPath, { recursive: true });
        }
        if (!fs.existsSync(this.utilities.packCachePath)) {
            fs.mkdirSync(this.utilities.packCachePath, { recursive: true });
        }
        __classPrivateFieldSet(this, _LocalEnvironment_prefsStorage, new NodeStorage_1.default(this.utilities.envPrefsPath, ""), "f");
        __classPrivateFieldSet(this, _LocalEnvironment_rootPrefFolder, __classPrivateFieldGet(this, _LocalEnvironment_prefsStorage, "f").rootFolder, "f");
        __classPrivateFieldSet(this, _LocalEnvironment_configFile, __classPrivateFieldGet(this, _LocalEnvironment_rootPrefFolder, "f").ensureFile("envprefs.json"), "f");
    }
    setWorldContainerPath(newPath) {
        if (newPath !== __classPrivateFieldGet(this, _LocalEnvironment_data, "f").worldContainerPath) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").worldContainerPath = newPath;
            if (newPath) {
                if (!fs.existsSync(newPath)) {
                    fs.mkdirSync(newPath, { recursive: true });
                }
                __classPrivateFieldSet(this, _LocalEnvironment_worldContainerStorage, new NodeStorage_1.default(newPath, ""), "f");
            }
        }
    }
    handleNewLogMessage(log, item) {
        if (item.level === Log_1.LogItemLevel.verbose && !this.displayVerbose) {
            return;
        }
        let context = "";
        if (this.displayVerbose) {
            context = worker_threads_1.threadId + ": ";
        }
        if (item.context && item.context.length > 0) {
            context = item.context + " ";
        }
        if (item.level === Log_1.LogItemLevel.verbose) {
            console.log(exports.consoleText_fgGray + context + item.message + exports.consoleText_reset);
        }
        else if (item.level === Log_1.LogItemLevel.error) {
            console.error(exports.consoleText_fgRed + context + "Error: " + item.message + exports.consoleText_reset);
        }
        else if (item.level === Log_1.LogItemLevel.important) {
            console.warn(exports.consoleText_fgYellow + context + "Important: " + item.message + exports.consoleText_reset);
        }
        else {
            console.log(context + item.message);
        }
    }
    async load() {
        if (__classPrivateFieldGet(this, _LocalEnvironment_isLoaded, "f")) {
            return;
        }
        await __classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").loadContent(false);
        if (__classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").content !== null &&
            __classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").content !== undefined &&
            typeof __classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").content === "string") {
            __classPrivateFieldSet(this, _LocalEnvironment_data, JSON.parse(__classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").content), "f");
        }
        __classPrivateFieldSet(this, _LocalEnvironment_isLoaded, true, "f");
    }
    async setDefaults() {
        await this.load();
        if (__classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverDomainName === undefined) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverDomainName = "localhost";
        }
        if (__classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverHostPort === undefined) {
            __classPrivateFieldGet(this, _LocalEnvironment_data, "f").serverHostPort = 6126;
        }
        if (this._inmemDisplayReadOnlyPasscode === undefined ||
            this._inmemDisplayReadOnlyPasscodeComplement === undefined) {
            this._inmemDisplayReadOnlyPasscode = this.generateRandomPasscode();
        }
        if (this._inmemAdminPasscodeComplement === undefined || this._inmemAdminPasscodeComplement === undefined) {
            this._inmemAdminPasscode = this.generateRandomPasscode();
        }
        if (this._inmemFullReadOnlyPasscode === undefined || this._inmemFullReadOnlyPasscodeComplement === undefined) {
            this.fullReadOnlyPasscode = this.generateRandomPasscode();
        }
        if (this._inmemUpdateStatePasscode === undefined || this._inmemUpdateStatePasscodeComplement === undefined) {
            this.updateStatePasscode = this.generateRandomPasscode();
        }
        await this.save();
    }
    generateRandomPasscode() {
        let newPasscode = "";
        for (let i = 0; i < 8; i++) {
            const rand = crypto.randomInt(34); // don't include 0 and 1 to preclude O/I confusion
            if (rand <= 7) {
                newPasscode += String.fromCharCode(rand + 50);
            }
            else {
                newPasscode += String.fromCharCode(rand + 89); // 89 + 8 = starts at A range.
            }
        }
        return newPasscode;
    }
    generateRandomTokenPassword() {
        let newPasscode = "";
        for (let i = 0; i < 12; i++) {
            const rand = crypto.randomInt(36);
            if (rand <= 9) {
                newPasscode += String.fromCharCode(rand + 48);
            }
            else {
                newPasscode += String.fromCharCode(rand + 87); // 87 + 10 = starts at A range.
            }
        }
        return newPasscode;
    }
    async save() {
        __classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").setContent(JSON.stringify(__classPrivateFieldGet(this, _LocalEnvironment_data, "f"), null, 2));
        __classPrivateFieldGet(this, _LocalEnvironment_configFile, "f").saveContent();
    }
}
exports.default = LocalEnvironment;
_LocalEnvironment_data = new WeakMap(), _LocalEnvironment_isLoaded = new WeakMap(), _LocalEnvironment_prefsStorage = new WeakMap(), _LocalEnvironment_rootPrefFolder = new WeakMap(), _LocalEnvironment_configFile = new WeakMap(), _LocalEnvironment_worldContainerStorage = new WeakMap(), _LocalEnvironment_displayInfo = new WeakMap(), _LocalEnvironment_displayVerbose = new WeakMap();

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