"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const ICartoData_1 = require("../app/ICartoData");
class EnvSettings {
    constructor() {
        this._isLoaded = false;
        this._onLoaded = new ste_events_1.EventDispatcher();
        this.project = undefined;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    static async ensureOnFile(file, loadHandler) {
        let envf;
        if (file.manager === undefined) {
            envf = new EnvSettings();
            envf.file = file;
            file.manager = envf;
        }
        if (file.manager !== undefined && file.manager instanceof EnvSettings) {
            envf = file.manager;
            if (!envf.isLoaded && loadHandler) {
                envf.onLoaded.subscribe(loadHandler);
            }
            await envf.load();
            return envf;
        }
        return envf;
    }
    async ensureEnvFile(project) {
        if (this._file === undefined) {
            return;
        }
        await this.load();
        let content = this._file?.content;
        if (content === null) {
            content = "";
        }
        if (content && typeof content === "string") {
        }
    }
    async persist() {
        if (this._file === undefined) {
            return;
        }
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        if (this._file.isContentLoaded && this.project && this._file.content && typeof this._file.content === "string") {
            this._file.setContent(await EnvSettings.getContent(this.project, this._file.content));
        }
        await this._file.saveContent(false);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent(true);
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this._isLoaded = true;
    }
    static async getContent(project, content) {
        if (content === undefined) {
            content = 'PROJECT_NAME=""\r\nMINECRAFT_PRODUCT="BedrockUWP"\r\nCUSTOM_DEPLOYMENT_PATH=""\r\n';
        }
        const folder = await project.getDefaultBehaviorPackFolder();
        if (folder) {
            let projectNameIndex = content.indexOf('PROJECT_NAME="');
            if (projectNameIndex >= 0) {
                let projectNameNewQuote = content.indexOf('"', projectNameIndex + 14);
                if (projectNameNewQuote > projectNameIndex) {
                    content =
                        content.substring(0, projectNameIndex) +
                            'PROJECT_NAME="' +
                            folder.name +
                            "" +
                            content.substring(projectNameNewQuote);
                }
            }
            else {
                if (!content.endsWith("\n")) {
                    content += "\r\n";
                }
                content = content + 'PROJECT_NAME="' + folder.name + '"\r\n';
            }
        }
        let minecraftProductIndex = content.indexOf('MINECRAFT_PRODUCT="');
        const trackStr = project.track === ICartoData_1.MinecraftTrack.preview ? "PreviewURP" : "BedrockUWP";
        if (minecraftProductIndex >= 0) {
            let minecraftProductIndexNextQuote = content.indexOf('"', minecraftProductIndex + 19);
            if (minecraftProductIndexNextQuote > minecraftProductIndex) {
                content =
                    content.substring(0, minecraftProductIndex) +
                        'MINECRAFT_PRODUCT="' +
                        trackStr +
                        content.substring(minecraftProductIndexNextQuote);
            }
        }
        else {
            if (!content.endsWith("\n")) {
                content += "\r\n";
            }
            content = content + 'MINECRAFT_PRODUCT="' + trackStr + '"\r\n';
        }
        return content;
    }
}
exports.default = EnvSettings;

//# sourceMappingURL=../maps/devproject/EnvSettings.js.map
