"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostType = exports.CartoThemeStyle = void 0;
const Carto_1 = require("./Carto");
const Database_1 = require("./../minecraft/Database");
const BrowserStorage_1 = require("./../storage/BrowserStorage");
const ste_events_1 = require("ste-events");
const GitHubManager_1 = require("../github/GitHubManager");
const AppServiceProxy_1 = require("../core/AppServiceProxy");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const CartoCommands_1 = require("./CartoCommands");
var CartoThemeStyle;
(function (CartoThemeStyle) {
    CartoThemeStyle[CartoThemeStyle["dark"] = 0] = "dark";
    CartoThemeStyle[CartoThemeStyle["light"] = 1] = "light";
    CartoThemeStyle[CartoThemeStyle["highContrast"] = 2] = "highContrast";
})(CartoThemeStyle = exports.CartoThemeStyle || (exports.CartoThemeStyle = {}));
var HostType;
(function (HostType) {
    HostType[HostType["web"] = 0] = "web";
    HostType[HostType["toolsNodejs"] = 1] = "toolsNodejs";
    HostType[HostType["electronNodeJs"] = 2] = "electronNodeJs";
    HostType[HostType["electronWeb"] = 3] = "electronWeb";
    HostType[HostType["vsCodeMainNodeJs"] = 4] = "vsCodeMainNodeJs";
    HostType[HostType["vsCodeMainWeb"] = 5] = "vsCodeMainWeb";
    HostType[HostType["vsCodeWebService"] = 6] = "vsCodeWebService";
    HostType[HostType["vsCodeWebWeb"] = 7] = "vsCodeWebWeb";
    HostType[HostType["webPlusServices"] = 8] = "webPlusServices";
    HostType[HostType["testLocal"] = 9] = "testLocal";
})(HostType = exports.HostType || (exports.HostType = {}));
/// CartoApp mostly serves as a static app loader for the really-core Carto app object.
class CartoApp {
    static get isNodeJs() {
        return false;
    }
    static get isVsCode() {
        return false;
    }
    static get isWeb() {
        return (this.hostType === HostType.webPlusServices ||
            this.hostType === HostType.web ||
            this.hostType === HostType.electronWeb);
    }
    static get isAppServiceWeb() {
        return this.hostType === HostType.electronWeb || this.hostType === HostType.vsCodeMainWeb;
    }
    static get onMessage() {
        return CartoApp._onMessage.asEvent();
    }
    static get onInitialized() {
        return CartoApp._onInitialized.asEvent();
    }
    static get carto() {
        return CartoApp._carto;
    }
    static notifyNewMessage(source, message) {
        CartoApp._onMessage.dispatch(source, message);
    }
    static setHostType(type) {
        this.hostType = type;
    }
    static init() {
        if (CartoApp._initializing || CartoApp._initialized) {
            return;
        }
        CartoApp._initializing = true;
        //@ts-ignore
        if (typeof g_contentRoot !== "undefined") {
            //@ts-ignore
            CartoApp.contentRoot = StorageUtilities_1.default.ensureEndsWithDelimiter(g_contentRoot);
        }
        //@ts-ignore
        if (typeof g_initialMode !== "undefined") {
            //@ts-ignore
            CartoApp.initialMode = g_initialMode;
        }
        //@ts-ignore
        if (typeof g_modeParameter !== "undefined") {
            //@ts-ignore
            CartoApp.modeParameter = g_modeParameter;
        }
        //@ts-ignore
        if (typeof g_projectPath !== "undefined") {
            //@ts-ignore
            CartoApp.projectPath = g_projectPath;
        }
        //@ts-ignore
        if (typeof g_baseUrl !== "undefined") {
            //@ts-ignore
            CartoApp.baseUrl = g_baseUrl;
        }
        AppServiceProxy_1.default.init();
        GitHubManager_1.default.init();
        Database_1.default.load();
        CartoCommands_1.default.registerCommonCommands();
        CartoApp.generateCryptoRandomNumber = (toVal) => {
            if (!CartoApp.carto || !CartoApp.carto.local) {
                throw new Error("Could not generate key.");
            }
            return CartoApp.carto.local.generateCryptoRandomNumber(toVal);
        };
        if (CartoApp.projectsStorage !== null && CartoApp.prefsStorage !== null) {
            // @ts-ignore
        }
        else if (typeof window !== "undefined") {
            CartoApp.prefsStorage = new BrowserStorage_1.default("mctprefs");
            CartoApp.projectsStorage = new BrowserStorage_1.default("mctprojects");
            CartoApp.deploymentStorage = new BrowserStorage_1.default("mctdeploy");
            CartoApp.workingStorage = new BrowserStorage_1.default("mctworking");
            CartoApp.worldStorage = new BrowserStorage_1.default("mctworlds");
            CartoApp.packStorage = new BrowserStorage_1.default("mctpacks");
            CartoApp.generateCryptoRandomNumber = (toVal) => {
                // @ts-ignore
                return window.crypto.getRandomValues(new Uint32Array(1))[0] % 16;
            };
        }
        if (CartoApp.prefsStorage === null || CartoApp.projectsStorage === null) {
            throw new Error("Unexpected uninitialized storage.");
        }
        CartoApp._carto = new Carto_1.default(CartoApp.prefsStorage, CartoApp.projectsStorage, CartoApp.deploymentStorage, CartoApp.previewDeploymentStorage, CartoApp.worldStorage, CartoApp.packStorage, CartoApp.workingStorage, CartoApp.contentRoot);
        if (CartoApp.ensureLocalFolder) {
            CartoApp._carto.ensureLocalFolder = CartoApp.ensureLocalFolder;
            CartoApp._carto.localFolderExists = CartoApp.localFolderExists;
            CartoApp._carto.localFileExists = CartoApp.localFileExists;
        }
        CartoApp._carto.createMinecraft = CartoApp.createMinecraft;
        CartoApp._carto.canCreateMinecraft = CartoApp.canCreateMinecraft;
        CartoApp._initialized = true;
        this._onInitialized.dispatch(CartoApp._carto, CartoApp._carto);
    }
}
exports.default = CartoApp;
CartoApp._onInitialized = new ste_events_1.EventDispatcher();
CartoApp._initializing = false;
CartoApp._initialized = false;
CartoApp.isLocalNode = false;
CartoApp.hostType = HostType.web;
CartoApp.hostManager = undefined;
CartoApp.theme = CartoThemeStyle.dark;
CartoApp.contentRoot = "";
CartoApp.projectPath = "";
CartoApp.baseUrl = "";
CartoApp.initialMode = null;
CartoApp.modeParameter = null;
CartoApp.prefsStorage = null;
CartoApp.projectsStorage = null;
CartoApp.deploymentStorage = null;
CartoApp.previewDeploymentStorage = null;
CartoApp.worldStorage = null;
CartoApp.packStorage = null;
CartoApp.workingStorage = null;
CartoApp._onMessage = new ste_events_1.EventDispatcher();

//# sourceMappingURL=../maps/app/CartoApp.js.map
