"use strict";
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 {
    #usePreview;
    #httpServer;
    #carto;
    #utilities;
    #env;
    dataStorage;
    runOnce;
    maxServerIndex = 0;
    #features = ServerManagerFeatures.all;
    #isPrepared = false;
    primaryServerPort = 19132;
    backupWorldFileListings = {};
    #onShutdown = new ste_events_1.EventDispatcher();
    get isAnyServerRunning() {
        return false;
    }
    get carto() {
        return this.#carto;
    }
    get features() {
        return this.#features;
    }
    set features(featuresIn) {
        this.#features = featuresIn;
    }
    get onShutdown() {
        return this.#onShutdown.asEvent();
    }
    get usePreview() {
        return this.#usePreview;
    }
    set usePreview(newUsePreview) {
        this.#usePreview = newUsePreview;
    }
    constructor(env, carto) {
        this.#utilities = env.utilities;
        this.#env = env;
        this.#carto = carto;
        this.dataStorage = new NodeStorage_1.default(this.getRootPath() + "data/", "");
    }
    stopWebServer() {
        if (this.#httpServer) {
            this.#httpServer.stop();
        }
    }
    async shutdown(message) {
        this.stopWebServer();
        if (this.#onShutdown) {
            this.#onShutdown.dispatch(this, message);
        }
    }
    ensureHttpServer() {
        if (!this.#httpServer) {
            this.#httpServer = new HttpServer_1.default(this.#env, this);
            this.#httpServer.carto = this.#carto;
            this.#httpServer.init();
        }
        return this.#httpServer;
    }
    get environment() {
        return this.#env;
    }
    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 this.#usePreview || (this.#carto && this.#carto.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 && this.#isPrepared) {
            return;
        }
        this.#isPrepared = true;
    }
    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;

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