"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var GenerationTree_1 = require("./GenerationTree");
var SceneSnapshot = /** @class */ (function () {
    function SceneSnapshot(options) {
        this.snapshot = GenerationTree_1.buildGenerationTree(options);
        this.apiUtil = options.apiUtil;
    }
    /**
     * Replaces all references to a comId with another one inside the snapshot
     *
     * @param {string} targetComId
     * @param {string} overwriteComId
     * @returns
     */
    SceneSnapshot.prototype.replaceComId = function (targetComId, overwriteComId) {
        var e_1, _a, e_2, _b;
        var node = this.getNode(targetComId);
        if (node === undefined) {
            return;
        }
        node.comId = overwriteComId;
        this.snapshot.set(overwriteComId, node);
        this.snapshot.delete(targetComId);
        if (node.children !== undefined) {
            try {
                for (var _c = tslib_1.__values(node.children), _d = _c.next(); !_d.done; _d = _c.next()) {
                    var childComId = _d.value;
                    var childNode = this.getNode(childComId);
                    if (childNode.snapPoint !== undefined) {
                        childNode.snapPoint.foreignComId = overwriteComId;
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                }
                finally { if (e_1) throw e_1.error; }
            }
        }
        try {
            for (var _e = tslib_1.__values(this.snapshot), _f = _e.next(); !_f.done; _f = _e.next()) {
                var _g = tslib_1.__read(_f.value, 2), treeNode = _g[1];
                if (treeNode.children !== undefined) {
                    for (var index = 0; index < treeNode.children.length; index++) {
                        if (treeNode.children[index] === targetComId) {
                            treeNode.children[index] = overwriteComId;
                            return;
                        }
                    }
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    SceneSnapshot.prototype.getRootElements = function () {
        return this.snapshot.get(GenerationTree_1.GENERATION_TREE_ROOT_NAME).children;
    };
    SceneSnapshot.prototype.getNode = function (comId) {
        return this.snapshot.get(comId);
    };
    SceneSnapshot.prototype.getNewConnectionInfo = function (comId) {
        var node = this.snapshot.get(comId);
        if (node === undefined || node.snapPoint === undefined) {
            return "none";
        }
        var parentPoint = this.apiUtil.getPointByKey(node.snapPoint.foreignComId, node.snapPoint.foreignPointKey);
        var ownPoint = this.apiUtil.getPointByKey(comId, node.snapPoint.ownPointKey);
        if (ownPoint === undefined || parentPoint === undefined) {
            return "missing";
        }
        return {
            parentPoint: parentPoint,
            parentComId: node.snapPoint.foreignComId,
            ownPoint: ownPoint,
            ownComId: comId,
        };
    };
    SceneSnapshot.prototype.restorePositionRecursively = function (comId) {
        var e_3, _a;
        this.restorePosition(comId);
        var node = this.getNode(comId);
        if (node && node.children) {
            try {
                for (var _b = tslib_1.__values(node.children), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var child = _c.value;
                    this.restorePositionRecursively(child);
                }
            }
            catch (e_3_1) { e_3 = { error: e_3_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                }
                finally { if (e_3) throw e_3.error; }
            }
        }
    };
    SceneSnapshot.prototype.restorePosition = function (comId) {
        var connectionInfo = this.getNewConnectionInfo(comId);
        if (connectionInfo === "none" || connectionInfo === "missing") {
            return false;
        }
        this.apiUtil.placeArticleAtAttachPoint(connectionInfo.parentComId, connectionInfo.ownComId, connectionInfo.parentPoint, connectionInfo.ownPoint);
        return true;
    };
    return SceneSnapshot;
}());
exports.SceneSnapshot = SceneSnapshot;
