"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");
const Database_1 = require("./Database");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
class ModelGeometryDefinition {
    constructor() {
        this._isLoaded = false;
        this.definitions = [];
        this._identifiers = [];
        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 identifiers() {
        if (this._identifiers) {
            return this._identifiers;
        }
        if (!this.wrapper ||
            !this.wrapper["minecraft:geometry"] ||
            this.wrapper["minecraft:geometry"].length !== 1 ||
            !this.wrapper["minecraft:geometry"][0].description) {
            return [];
        }
        const ids = [];
        for (const def of this.definitions) {
            if (def.description && def.description.identifier) {
                ids.push(def.description.identifier);
            }
        }
        return ids;
    }
    getVisibleBoundsWidth(defIndex) {
        if (defIndex < 0 || defIndex >= this.definitions.length) {
            return;
        }
        if (!this.definitions[defIndex]) {
            return undefined;
        }
        if (this.definitions[defIndex].description) {
            return this.definitions[defIndex].description.visible_bounds_width;
        }
        return this.definitions[defIndex].visible_bounds_width;
    }
    getVisibleBoundsHeight(defIndex) {
        if (defIndex < 0 || defIndex >= this.definitions.length) {
            return;
        }
        if (!this.definitions[defIndex]) {
            return undefined;
        }
        if (this.definitions[defIndex].description) {
            return this.definitions[defIndex].description.visible_bounds_height;
        }
        return this.definitions[defIndex].visible_bounds_height;
    }
    getVisibleBoundsOffset(defIndex) {
        if (defIndex < 0 || defIndex >= this.definitions.length) {
            return;
        }
        if (!this.definitions[defIndex]) {
            return undefined;
        }
        if (this.definitions[defIndex].description) {
            return this.definitions[defIndex].description.visible_bounds_offset;
        }
        return this.definitions[defIndex].visible_bounds_offset;
    }
    getTextureWidth(defIndex) {
        if (defIndex < 0 || defIndex >= this.definitions.length) {
            return;
        }
        if (!this.definitions[defIndex]) {
            return undefined;
        }
        if (this.definitions[defIndex].description) {
            return this.definitions[defIndex].description.texture_width;
        }
        return this.definitions[defIndex].texturewidth;
    }
    getTextureHeight(defIndex) {
        if (defIndex < 0 || defIndex >= this.definitions.length) {
            return;
        }
        if (!this.definitions[defIndex]) {
            return undefined;
        }
        if (this.definitions[defIndex].description) {
            return this.definitions[defIndex].description.texture_height;
        }
        return this.definitions[defIndex].textureheight;
    }
    static async ensureOnFile(file, loadHandler) {
        let rc;
        if (file.manager === undefined) {
            rc = new ModelGeometryDefinition();
            rc.file = file;
            file.manager = rc;
        }
        if (file.manager !== undefined && file.manager instanceof ModelGeometryDefinition) {
            rc = file.manager;
            if (!rc.isLoaded && loadHandler) {
                rc.onLoaded.subscribe(loadHandler);
            }
            await rc.load();
        }
        return rc;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return await Database_1.default.isRecentVersionFromVersionArray(fv);
    }
    getFormatVersion() {
        if (!this.wrapper || !this.wrapper.format_version) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this.wrapper.format_version);
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const pjString = JSON.stringify(this.wrapper, null, 2);
        this._file.setContent(pjString);
    }
    ensureDefinition(name) {
        if (!this.wrapper) {
            this.wrapper = {
                format_version: "1.12.0",
                "minecraft:geometry": [
                    {
                        description: {
                            identifier: name,
                            texture_width: 32,
                            texture_height: 32,
                            visible_bounds_width: 2,
                            visible_bounds_height: 2,
                            visible_bounds_offset: [0, 0, 0],
                        },
                        bones: [],
                    },
                ],
            };
        }
    }
    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.wrapper = StorageUtilities_1.default.getJsonObject(this._file);
        this.definitions = [];
        if (this.wrapper && this.wrapper["minecraft:geometry"]) {
            for (const def of this.wrapper["minecraft:geometry"]) {
                this.definitions.push(def);
            }
        }
        else if (this.wrapper) {
            // look for 1.8.0 style geometries:
            // {
            //   "format_version": ...
            //   "geometry.foobar": {}
            // }
            for (const elt in this.wrapper) {
                if (elt !== "format_version" && elt.startsWith("geometry.") && this.wrapper[elt]) {
                    this._identifiers.push(elt);
                    this.definitions.push(this.wrapper[elt]);
                }
            }
        }
        this._isLoaded = true;
    }
}
exports.default = ModelGeometryDefinition;

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