"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");
class AttachableResourceDefinition {
    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.attachableWrapper ||
            !this.attachableWrapper["minecraft:attachable"] ||
            !this.attachableWrapper["minecraft:attachable"].description) {
            return undefined;
        }
        return this.attachableWrapper["minecraft:attachable"].description.identifier;
    }
    getFormatVersion() {
        if (!this.attachableWrapper) {
            return undefined;
        }
        const fv = this.attachableWrapper.format_version;
        if (typeof fv === "number") {
            return [fv];
        }
        if (typeof fv === "string") {
            let fvarr = this.attachableWrapper.format_version.split(".");
            let fvarrInt = [];
            for (let i = 0; i < fvarr.length; i++) {
                try {
                    fvarrInt.push(parseInt(fvarr[i]));
                }
                catch (e) { }
            }
            return fvarrInt;
        }
        return undefined;
    }
    get formatVersion() {
        if (!this.attachableWrapper || !this.attachableWrapper.format_version) {
            return undefined;
        }
        return this.attachableWrapper.format_version;
    }
    static async ensureOnFile(file, loadHandler) {
        let attachable;
        if (file.manager === undefined) {
            attachable = new AttachableResourceDefinition();
            attachable.file = file;
            file.manager = attachable;
        }
        if (file.manager !== undefined && file.manager instanceof AttachableResourceDefinition) {
            attachable = file.manager;
            if (!attachable.isLoaded && loadHandler) {
                attachable.onLoaded.subscribe(loadHandler);
            }
            await attachable.load();
        }
        return attachable;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const defString = JSON.stringify(this.attachableWrapper, null, 2);
        this._file.setContent(defString);
    }
    async load() {
        if (this._isLoaded) {
            return;
        }
        if (this._file === undefined) {
            Log_1.default.unexpectedUndefined("ATTRPF");
            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.attachableWrapper = data;
        this._isLoaded = true;
        this._onLoaded.dispatch(this, this);
    }
}
exports.default = AttachableResourceDefinition;

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