"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var lodash_1 = tslib_1.__importDefault(require("lodash"));
var Core = tslib_1.__importStar(require("@intelligentgraphics/ig.gfx.core"));
/**
 * Singleton class that contains all relevant information about the Environment (room, walls, floor).
 * Note that the structure of the environment is like this:
 * tx__. : Environment root
 * tx__o1: Environment subroot
 * tx__o1 - tx__oy: Floors and wall, distinguished by the categories Floor / Wall
 */
var Environment = /** @class */ (function () {
    function Environment() {
        this.root = "";
        this.floor = "";
        this.environment = "";
        this.walls = [];
        var hasCategory = function (cat) { return function (id) {
            return Core.State.API.GetCategories(id).some(function (category) { return category === cat; });
        }; };
        var isEnvironment = hasCategory(Core.Category.Environment);
        var isFloor = hasCategory(Core.Category.Floor);
        var isWall = hasCategory(Core.Category.Wall);
        this.root = lodash_1.default(Core.State.API.GetRoots()).find(function (id) {
            return isEnvironment(id) ||
                Core.State.API.GetChildren(id).some(isEnvironment);
        });
        if (this.root) {
            this.environment = lodash_1.default(Core.State.API.GetChildren(this.root)).find(isEnvironment);
            this.floor = lodash_1.default(Core.State.API.GetChildren(this.environment)).find(isFloor);
            this.walls = Core.State.API.GetChildren(this.environment).filter(isWall);
        }
    }
    Environment.prototype.getRoot = function () {
        return this.root;
    };
    Environment.prototype.getFloor = function () {
        return this.floor;
    };
    Environment.prototype.getEnvironment = function () {
        return this.environment;
    };
    Environment.prototype.getWalls = function () {
        return this.walls;
    };
    Environment.setInstance = function (val) {
        Environment.__instance = val;
    };
    Environment.getInstance = function () {
        // if instance has not yet been initialized, or the environment has changed we need to update the singleton
        if (!Environment.__instance ||
            !lodash_1.default.includes(Core.State.API.GetRoots(), Environment.__instance.getRoot())) {
            Environment.__instance = new Environment();
        }
        return Environment.__instance;
    };
    // gets lowest level named Objects eg ["t1__o1.o8.o4"] for the respective parts of the wall
    Environment.prototype.getWallFronts = function () {
        var tResult = { fronts: [], others: [] };
        var tFloor = this.getFloor();
        if (tFloor) {
            tResult.others.push(tFloor);
        }
        var tWalls = this.getWalls();
        tWalls.forEach(function (W) {
            var tPartIds = Core.State.API.GetChildren(W);
            tPartIds.forEach(function (part) {
                var pcomp = Core.State.API.GetComponent(part);
                if (pcomp) {
                    var tFoundInterior_1 = false;
                    pcomp.traverse(function (p) {
                        if (p["SpecialMeshName"] === "Interior") {
                            tFoundInterior_1 = true;
                        }
                    });
                    if (tFoundInterior_1) {
                        tResult.fronts.push(part);
                    }
                    else {
                        tResult.others.push(part);
                    }
                }
            });
        });
        return tResult;
    };
    return Environment;
}());
exports.default = Environment;
