"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var IGXC = tslib_1.__importStar(require("../IGXC"));
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var PATH_DELIMITER = ".";
exports.buildObjectTree = function (objects) {
    var e_1, _a;
    var rootObject = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, IGXC.createObject(IGXC.OBJECT_ROOT_PATH)), objects.find(function (obj) { return obj.Path === IGXC.OBJECT_ROOT_PATH; })), { Children: [] });
    var children = objects.slice(1);
    try {
        for (var children_1 = tslib_1.__values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
            var entry = children_1_1.value;
            var pathSegments = entry.Path.split(PATH_DELIMITER);
            var parent_1 = rootObject;
            var _loop_1 = function (index) {
                var parentName = pathSegments
                    .slice(0, index + 1)
                    .join(PATH_DELIMITER);
                parent_1 = parent_1.Children.find(function (entry) { return entry.Path === parentName; });
            };
            for (var index = 0; index < pathSegments.length - 1; index++) {
                _loop_1(index);
            }
            parent_1.Children.push(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, IGXC.createObject(entry.Path)), ig_utilities_1.Obj.deepClone(entry)), { Children: [] }));
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return rootObject;
};
