"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Carto_1 = require("./Carto");
const IMinecraft_1 = require("./IMinecraft");
const AppServiceProxy_1 = require("../core/AppServiceProxy");
const ste_events_1 = require("ste-events");
const Utilities_1 = require("../core/Utilities");
const GameStateManager_1 = require("../minecraft/GameStateManager");
const Log_1 = require("../core/Log");
const ICartoData_1 = require("./ICartoData");
const ProjectExporter_1 = require("./ProjectExporter");
class DeploymentStorageMinecraft {
    _carto;
    state;
    _onStateChanged = new ste_events_1.EventDispatcher();
    _onRefreshed = new ste_events_1.EventDispatcher();
    _project;
    _gameStateManager;
    errorStatus;
    errorMessage;
    worldFolder;
    projectFolder;
    _onWorldFolderReady = new ste_events_1.EventDispatcher();
    _onProjectFolderReady = new ste_events_1.EventDispatcher();
    _onMessage = new ste_events_1.EventDispatcher();
    get onWorldFolderReady() {
        return this._onWorldFolderReady.asEvent();
    }
    get onProjectFolderReady() {
        return this._onProjectFolderReady.asEvent();
    }
    get onMessage() {
        return this._onMessage.asEvent();
    }
    get canDeployFiles() {
        return true;
    }
    get onRefreshed() {
        return this._onRefreshed.asEvent();
    }
    get onStateChanged() {
        return this._onStateChanged.asEvent();
    }
    constructor(carto) {
        this._carto = carto;
        this._gameStateManager = new GameStateManager_1.default(this._carto);
        this.state = Carto_1.CartoMinecraftState.none;
    }
    async updateStatus() {
        return this.state;
    }
    get activeProject() {
        return this._project;
    }
    set activeProject(newProject) {
        this._project = newProject;
    }
    get gameStateManager() {
        return this._gameStateManager;
    }
    notifyStateChanged(newVal) {
        this.state = newVal;
        this._onStateChanged.dispatch(this, newVal);
    }
    async initialize() {
        await this.start();
    }
    async prepare(force) {
        return;
    }
    async runCommand(command) {
        return undefined;
    }
    processExternalMessage(command, data) { }
    async pushWorld() {
        if (!this._carto || this._carto.deploymentStorage === null || !this._project) {
            return;
        }
        let name = this._project.name + "_mct";
        const worldsFolder = await ProjectExporter_1.default.ensureMinecraftWorldsFolder(this._carto);
        if (!worldsFolder) {
            Log_1.default.debug("Could not find a Minecraft world.");
            return;
        }
        const worldFolder = worldsFolder.ensureFolder(name);
        await worldFolder.ensureExists();
        // Log.debugAlert("Exporting folder to '" + worldFolder.storageRelativePath + "'");
        await ProjectExporter_1.default.syncFlatPackRefWorldTo(this._carto, this._project, worldFolder, name);
        name = Utilities_1.default.getSimpleString(this._project.name) + "_mct";
        await worldFolder.saveAll();
        return name;
    }
    async syncWithDeployment() {
        if (this._carto.deploymentStorage == null ||
            this._carto.deployBehaviorPacksFolder == null ||
            this._carto.minecraftGameMode === ICartoData_1.MinecraftGameConnectionMode.remoteMinecraft) {
            throw new Error("This instance doesn't support deployment");
        }
        if (!this._project) {
            return;
        }
        let isAvailable = this._carto.deploymentStorage.available;
        if (isAvailable === undefined) {
            isAvailable = await this._carto.deploymentStorage.getAvailable();
        }
        if (!isAvailable) {
            return;
        }
        const deployFolderExists = await this._carto.deployBehaviorPacksFolder.exists();
        if (deployFolderExists) {
            await ProjectExporter_1.default.deployProject(this._carto, this._project, this._carto.deploymentStorage.rootFolder);
        }
    }
    canPrepare() {
        return true;
    }
    async prepareAndStart(push) {
        let worldName = undefined;
        if (push.project) {
            this._project = push.project;
            await this.syncWithDeployment();
        }
        if (push.worldType) {
            worldName = await this.pushWorld();
        }
        await this.start();
        return {
            type: IMinecraft_1.PrepareAndStartResultType.started,
            worldName: worldName,
        };
    }
    async stop() { }
    async start() {
        if (!AppServiceProxy_1.default.hasAppService && Utilities_1.default.isDebug) {
            this.notifyStateChanged(Carto_1.CartoMinecraftState.stopped);
        }
        else if (this.state === Carto_1.CartoMinecraftState.none ||
            this.state === Carto_1.CartoMinecraftState.error ||
            this.state === Carto_1.CartoMinecraftState.disconnected) {
            this.notifyStateChanged(Carto_1.CartoMinecraftState.initializing);
        }
    }
}
exports.default = DeploymentStorageMinecraft;

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