"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 StorageUtilities_1 = require("../storage/StorageUtilities");
class ResourceRenderController {
    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();
    }
    static async ensureOnFile(file, loadHandler) {
        let rc;
        if (file.manager === undefined) {
            rc = new ResourceRenderController();
            rc.file = file;
            file.manager = rc;
        }
        if (file.manager !== undefined && file.manager instanceof ResourceRenderController) {
            rc = file.manager;
            if (!rc.isLoaded && loadHandler) {
                rc.onLoaded.subscribe(loadHandler);
            }
            await rc.load();
        }
        return rc;
    }
    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: "1.12.0",
                render_controllers: {},
            };
        }
    }
    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.definition = StorageUtilities_1.default.getJsonObject(this._file);
        this._isLoaded = true;
    }
}
exports.default = ResourceRenderController;

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