"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");
const Log_1 = require("../core/Log");
class PersonaManifestDefinition {
    _file;
    _id;
    _isLoaded = false;
    definition;
    _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 packScope() {
        if (!this.definition || !this.definition.header) {
            return undefined;
        }
        return this.definition.header.pack_scope;
    }
    get productType() {
        if (!this.definition || !this.definition.metadata) {
            return undefined;
        }
        return this.definition.metadata.product_type;
    }
    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;
    }
    ensureHeaderForProject(project) {
        return this.ensureHeader(project.title, project.description);
    }
    get minEngineVersion() {
        if (!this.definition || !this.definition.header || !this.definition.header.min_engine_version) {
            return undefined;
        }
        return this.definition.header.min_engine_version;
    }
    setMinEngineVersion(versionArray, project) {
        const header = this.ensureHeaderForProject(project);
        header.min_engine_version = versionArray;
    }
    static async ensureOnFile(file, loadHandler) {
        let rmj;
        if (file.manager === undefined) {
            rmj = new PersonaManifestDefinition();
            rmj.file = file;
            file.manager = rmj;
        }
        if (file.manager !== undefined && file.manager instanceof PersonaManifestDefinition) {
            rmj = file.manager;
            if (!rmj.isLoaded) {
                if (loadHandler) {
                    rmj.onLoaded.subscribe(loadHandler);
                }
                await rmj.load();
            }
        }
        return rmj;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        if (!this.definition) {
            Log_1.default.unexpectedUndefined("ITRDP");
            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],
                    min_engine_version: [1, 20, 10],
                    uuid: Utilities_1.default.createUuid(),
                },
                modules: [],
                dependencies: [],
            };
        }
    }
    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(),
        };
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        this.persist();
        await this._file.saveContent(false);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        if (!this._file.isContentLoaded) {
            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 = PersonaManifestDefinition;

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