"use strict";
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 _ServerManager_usePreview, _ServerManager_httpServer, _ServerManager_carto, _ServerManager_utilities, _ServerManager_env, _ServerManager_features, _ServerManager_isPrepared, _ServerManager_onShutdown;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerManagerFeatures = void 0;
const ste_events_1 = require("ste-events");
const NodeStorage_1 = require("./NodeStorage");
const HttpServer_1 = require("./HttpServer");
const ICartoData_1 = require("../app/ICartoData");
var ServerManagerFeatures;
(function (ServerManagerFeatures) {
    ServerManagerFeatures[ServerManagerFeatures["all"] = 0] = "all";
    ServerManagerFeatures[ServerManagerFeatures["allWebServices"] = 1] = "allWebServices";
    ServerManagerFeatures[ServerManagerFeatures["basicWebServices"] = 2] = "basicWebServices";
})(ServerManagerFeatures = exports.ServerManagerFeatures || (exports.ServerManagerFeatures = {}));
class ServerManager {
    get isAnyServerRunning() {
        return false;
    }
    get carto() {
        return __classPrivateFieldGet(this, _ServerManager_carto, "f");
    }
    get features() {
        return __classPrivateFieldGet(this, _ServerManager_features, "f");
    }
    set features(featuresIn) {
        __classPrivateFieldSet(this, _ServerManager_features, featuresIn, "f");
    }
    get onShutdown() {
        return __classPrivateFieldGet(this, _ServerManager_onShutdown, "f").asEvent();
    }
    get usePreview() {
        return __classPrivateFieldGet(this, _ServerManager_usePreview, "f");
    }
    set usePreview(newUsePreview) {
        __classPrivateFieldSet(this, _ServerManager_usePreview, newUsePreview, "f");
    }
    constructor(env, carto) {
        _ServerManager_usePreview.set(this, void 0);
        _ServerManager_httpServer.set(this, void 0);
        _ServerManager_carto.set(this, void 0);
        _ServerManager_utilities.set(this, void 0);
        _ServerManager_env.set(this, void 0);
        this.maxServerIndex = 0;
        _ServerManager_features.set(this, ServerManagerFeatures.all);
        _ServerManager_isPrepared.set(this, false);
        this.primaryServerPort = 19132;
        this.backupWorldFileListings = {};
        _ServerManager_onShutdown.set(this, new ste_events_1.EventDispatcher());
        __classPrivateFieldSet(this, _ServerManager_utilities, env.utilities, "f");
        __classPrivateFieldSet(this, _ServerManager_env, env, "f");
        __classPrivateFieldSet(this, _ServerManager_carto, carto, "f");
        this.dataStorage = new NodeStorage_1.default(this.getRootPath() + "data/", "");
    }
    stopWebServer() {
        if (__classPrivateFieldGet(this, _ServerManager_httpServer, "f")) {
            __classPrivateFieldGet(this, _ServerManager_httpServer, "f").stop();
        }
    }
    async shutdown(message) {
        this.stopWebServer();
        if (__classPrivateFieldGet(this, _ServerManager_onShutdown, "f")) {
            __classPrivateFieldGet(this, _ServerManager_onShutdown, "f").dispatch(this, message);
        }
    }
    ensureHttpServer() {
        if (!__classPrivateFieldGet(this, _ServerManager_httpServer, "f")) {
            __classPrivateFieldSet(this, _ServerManager_httpServer, new HttpServer_1.default(__classPrivateFieldGet(this, _ServerManager_env, "f"), this), "f");
            __classPrivateFieldGet(this, _ServerManager_httpServer, "f").carto = __classPrivateFieldGet(this, _ServerManager_carto, "f");
            __classPrivateFieldGet(this, _ServerManager_httpServer, "f").init();
        }
        return __classPrivateFieldGet(this, _ServerManager_httpServer, "f");
    }
    get environment() {
        return __classPrivateFieldGet(this, _ServerManager_env, "f");
    }
    getRootPath() {
        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);
        }
        return fullPath;
    }
    get effectiveIsUsingPreview() {
        return __classPrivateFieldGet(this, _ServerManager_usePreview, "f") || (__classPrivateFieldGet(this, _ServerManager_carto, "f") && __classPrivateFieldGet(this, _ServerManager_carto, "f").track === ICartoData_1.MinecraftTrack.preview);
    }
    replaceVersion(versionString, stub) {
        if (versionString.endsWith(stub)) {
            return undefined;
        }
        const lastPeriod = versionString.lastIndexOf(".");
        if (lastPeriod >= 0) {
            versionString = versionString.substring(0, lastPeriod - 1) + stub;
            return versionString;
        }
        return undefined;
    }
    async prepare(force) {
        if (!force && __classPrivateFieldGet(this, _ServerManager_isPrepared, "f")) {
            return;
        }
        __classPrivateFieldSet(this, _ServerManager_isPrepared, true, "f");
    }
    getBasePortForSlot(slotNumber) {
        if (!slotNumber) {
            slotNumber = 0;
        }
        // assume if slotNumber > 79 they actually are suggesting a base port number
        if (slotNumber > 79) {
            return slotNumber;
        }
        // default minecraft base port = 19132, which is slot 0.
        return 19132 + slotNumber * 64;
    }
    getHashFromStartInfo(startInfo) {
        if (!startInfo) {
            return undefined;
        }
        let hash = "";
        if (startInfo.track) {
            hash += startInfo.track?.toString();
        }
        hash += ".";
        if (startInfo.mode) {
            hash += startInfo.mode;
        }
        hash += ".";
        if (startInfo.projectKey) {
            hash += startInfo.projectKey;
        }
        hash += ".";
        if (startInfo.worldSettings) {
            hash += JSON.stringify(startInfo.worldSettings);
        }
        return hash;
    }
    register() { }
}
exports.default = ServerManager;
_ServerManager_usePreview = new WeakMap(), _ServerManager_httpServer = new WeakMap(), _ServerManager_carto = new WeakMap(), _ServerManager_utilities = new WeakMap(), _ServerManager_env = new WeakMap(), _ServerManager_features = new WeakMap(), _ServerManager_isPrepared = new WeakMap(), _ServerManager_onShutdown = new WeakMap();

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