"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
class ParticleEffectResourceDefinition {
    constructor() {
        this._isLoaded = false;
        this._onLoaded = new ste_events_1.EventDispatcher();
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    get id() {
        if (!this.wrapper || !this.wrapper.particle_effect || !this.wrapper.particle_effect.description) {
            return undefined;
        }
        return this.wrapper.particle_effect.description.identifier;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return fv[0] > 1 || fv[1] >= 10;
    }
    getFormatVersion() {
        if (!this.wrapper) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this.wrapper.format_version);
    }
    get formatVersion() {
        if (!this.wrapper || !this.wrapper.format_version) {
            return undefined;
        }
        return this.wrapper.format_version;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new ParticleEffectResourceDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof ParticleEffectResourceDefinition) {
            et = file.manager;
            if (!et.isLoaded && loadHandler) {
                et.onLoaded.subscribe(loadHandler);
            }
            await et.load();
        }
        return et;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const defString = JSON.stringify(this.wrapper, null, 2);
        this._file.setContent(defString);
    }
    async load() {
        if (this._isLoaded) {
            return;
        }
        if (this._file === undefined) {
            Log_1.default.unexpectedUndefined("PERPF");
            return;
        }
        await this._file.loadContent();
        if (!this._file.content || this._file.content instanceof Uint8Array) {
            return;
        }
        let data = {};
        let result = StorageUtilities_1.default.getJsonObject(this._file);
        if (result) {
            data = result;
        }
        this.wrapper = data;
        this._isLoaded = true;
        this._onLoaded.dispatch(this, this);
    }
}
exports.default = ParticleEffectResourceDefinition;

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