"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 DataFormProcessor_1 = require("../dataform/DataFormProcessor");
const Database_1 = require("./Database");
class RenderControllerSetDefinition {
    _file;
    _isLoaded = false;
    _data;
    _onLoaded = new ste_events_1.EventDispatcher();
    id;
    get data() {
        return this._data;
    }
    get idList() {
        if (!this._data || !this._data.render_controllers) {
            return undefined;
        }
        const idList = new Set();
        for (const key in this._data.render_controllers) {
            const rc = this._data.render_controllers[key];
            if (key && rc) {
                idList.add(key);
            }
        }
        return idList;
    }
    get renderControllers() {
        if (!this._data || !this._data.render_controllers) {
            return undefined;
        }
        const rcList = [];
        for (const key in this._data.render_controllers) {
            const rc = this._data.render_controllers[key];
            if (key && rc) {
                rcList.push(rc);
            }
        }
        return rcList;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    async removeTexture(textureId) {
        await this.load();
        const rcs = this._data?.render_controllers;
        if (!rcs) {
            return;
        }
        for (const rcKey in rcs) {
            const rc = rcs[rcKey];
            if (rc) {
                if (rc.arrays.textures) {
                    for (const textureListName in rc.arrays.textures) {
                        const textureList = rc.arrays.textures[textureListName];
                        let newTextureList = [];
                        if (textureList) {
                            for (const textureStr of textureList) {
                                if (textureStr !== textureId && textureStr !== "Texture." + textureId) {
                                    newTextureList.push(textureStr);
                                }
                            }
                        }
                        if (newTextureList.length === 0) {
                            newTextureList = undefined;
                        }
                        rc.arrays.textures[textureListName] = newTextureList;
                    }
                }
            }
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let rc;
        if (file.manager === undefined) {
            rc = new RenderControllerSetDefinition();
            rc.file = file;
            file.manager = rc;
        }
        if (file.manager !== undefined && file.manager instanceof RenderControllerSetDefinition) {
            rc = file.manager;
            if (!rc.isLoaded) {
                if (loadHandler) {
                    rc.onLoaded.subscribe(loadHandler);
                }
                await rc.load();
            }
        }
        return rc;
    }
    async persist() {
        if (this._file === undefined) {
            return;
        }
        if (this._data) {
            const renderControllerSet = await Database_1.default.ensureFormLoaded("resource", "render_controller_set");
            if (renderControllerSet) {
                await DataFormProcessor_1.default.process(this._data, renderControllerSet, DataFormProcessor_1.ProcessorFixupLevel.perField);
            }
        }
        const pjString = JSON.stringify(this._data, null, 2);
        this._file.setContent(pjString);
    }
    ensureDefinition(name, description) {
        if (!this._data) {
            this._data = {
                format_version: "1.12.0",
                render_controllers: {},
            };
        }
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        await this.persist();
        await this._file.saveContent(false);
    }
    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._isLoaded = true;
    }
}
exports.default = RenderControllerSetDefinition;

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