"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");
const Log_1 = require("../core/Log");
class ModelGeometryDefinition {
    _file;
    _isLoaded = false;
    _data;
    definitions = [];
    _identifiers = [];
    _onLoaded = new ste_events_1.EventDispatcher();
    get data() {
        return this._data;
    }
    get formatVersion() {
        return this._data?.format_version;
    }
    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._data ||
            !this._data["minecraft:geometry"] ||
            this._data["minecraft:geometry"].length !== 1 ||
            !this._data["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;
    }
    getById(id) {
        Log_1.default.assert(id !== "minecraft:geometry");
        let model = this._data[id];
        if (model) {
            return model;
        }
        for (const def of this.definitions) {
            if (def.description && def.description.identifier === id) {
                return def;
            }
        }
        return undefined;
    }
    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;
    }
    ensureDefault(id) {
        if (!this._data) {
            this._data = {
                format_version: "1.12.0",
                "minecraft:geometry": [
                    {
                        description: {
                            identifier: id,
                            texture_width: 128,
                            texture_height: 64,
                            visible_bounds_width: 4,
                            visible_bounds_height: 3.5,
                            visible_bounds_offset: [0, 1.25, 0],
                        },
                        bones: [],
                    },
                ],
            };
            this.persist();
            this.populateDefsAndIds();
            this._isLoaded = true;
        }
    }
    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) {
                if (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._data || !this._data.format_version) {
            return [0, 0, 0];
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this._data.format_version);
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        if (!this._data) {
            Log_1.default.unexpectedUndefined("MGDP");
            return;
        }
        const pjString = JSON.stringify(this._data, null, 2);
        this._file.setContent(pjString);
    }
    ensureDefinition(name) {
        if (!this._data) {
            this._data = {
                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);
    }
    populateDefsAndIds() {
        this.definitions = [];
        this._identifiers = [];
        if (this._data && this._data["minecraft:geometry"]) {
            for (const def of this._data["minecraft:geometry"]) {
                if (def.description && def.description.identifier) {
                    this._identifiers.push(def.description.identifier);
                }
                this.definitions.push(def);
            }
        }
        else if (this._data) {
            // look for 1.8.0 style geometries:
            // {
            //   "format_version": ...
            //   "geometry.foobar": {}
            // }
            for (const elt in this._data) {
                if (elt !== "format_version" && elt.startsWith("geometry.") && this._data[elt]) {
                    this._identifiers.push(elt);
                    this.definitions.push(this._data[elt]);
                }
            }
        }
    }
    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._data = StorageUtilities_1.default.getJsonObject(this._file);
        this.populateDefsAndIds();
        this._isLoaded = true;
    }
}
exports.default = ModelGeometryDefinition;

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