"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 Utilities_1 = require("../core/Utilities");
const StorageUtilities_1 = require("../storage/StorageUtilities");
class WorldTemplateManifestDefinition {
    constructor() {
        this._isLoaded = false;
        this._onLoaded = new ste_events_1.EventDispatcher();
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get description() {
        if (!this.definition || !this.definition.header) {
            return undefined;
        }
        return this.definition.header.description;
    }
    set description(newDescription) {
        if (this.definition && this.definition.header && newDescription) {
            this.definition.header.description = newDescription;
        }
    }
    get name() {
        if (this.definition && this.definition.header) {
            return this.definition.header.name;
        }
        return undefined;
    }
    set name(newName) {
        if (this.definition && this.definition.header && newName) {
            this.definition.header.name = newName;
        }
    }
    get uuid() {
        if (this.definition && this.definition.header) {
            return this.definition.header.uuid;
        }
        return this._id;
    }
    set uuid(newId) {
        if (this.definition && this.definition.header && newId) {
            this.definition.header.uuid = newId;
        }
        this._id = newId;
    }
    static async ensureOnFile(file, loadHandler) {
        let bmj;
        if (file.manager === undefined) {
            bmj = new WorldTemplateManifestDefinition();
            bmj.file = file;
            file.manager = bmj;
        }
        if (file.manager !== undefined && file.manager instanceof WorldTemplateManifestDefinition) {
            bmj = file.manager;
            if (!bmj.isLoaded && loadHandler) {
                bmj.onLoaded.subscribe(loadHandler);
            }
            await bmj.load();
        }
        return bmj;
    }
    get baseGameVersion() {
        if (!this.definition || !this.definition.header || !this.definition.header.base_game_version) {
            return undefined;
        }
        return this.definition.header.base_game_version;
    }
    setBaseGameVersion(versionArray, project) {
        const header = this.ensureHeaderForProject(project);
        header.base_game_version = versionArray;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const pjString = JSON.stringify(this.definition, null, 2);
        this._file.setContent(pjString);
    }
    ensureDefinition(name, description) {
        if (!this.definition) {
            this.definition = {
                format_version: 2,
                header: {
                    name: name,
                    description: description,
                    version: [0, 0, 1],
                    base_game_version: [1, 20, 10],
                    uuid: Utilities_1.default.createUuid(),
                },
                modules: [],
            };
        }
    }
    ensureHeaderForProject(project) {
        return this.ensureHeader(project.title, project.description);
    }
    ensureHeader(name, description) {
        this.ensureDefinition(name, description);
        if (!this.definition) {
            throw new Error();
        }
        if (!this.definition.header) {
            this.definition.header = this.getDefaultHeader(name, description);
        }
        return this.definition.header;
    }
    getDefaultHeader(name, description) {
        return {
            name: name,
            description: description,
            version: [0, 0, 1],
            min_engine_version: [1, 20, 10],
            uuid: Utilities_1.default.createUuid(),
            base_game_version: [1, 20, 10],
        };
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        this.persist();
        await this._file.saveContent(false);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.uuid = this._file.name;
        this.definition = StorageUtilities_1.default.getJsonObject(this._file);
        this._isLoaded = true;
    }
}
exports.default = WorldTemplateManifestDefinition;

//# sourceMappingURL=../maps/minecraft/WorldTemplateManifestDefinition.js.map
