"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var ig_gfx_threejs_1 = require("@intelligentgraphics/ig.gfx.threejs");
var Core = require("@intelligentgraphics/ig.gfx.core");
var THREE = require("@intelligentgraphics/three");
var MultiArticle = require("@intelligentgraphics/ig.gfx.multiarticle");
var Planning = require("@intelligentgraphics/ig.gfx.planning");
var Util_1 = require("./Util");
var Logger_1 = require("./Logger");
var MultiArticleExecutor_1 = require("./MultiArticleExecutor");
var Logger = Logger_1.default.createSubLogger("ArticleInserter");
var evaluateMultiArticlePoints = function (attachPoints, subRootName, properties) {
    var e_1, _a;
    var evalConfiguration = tslib_1.__assign({ BBOX: MultiArticle.Rule.createBoundingBoxForRule(subRootName) }, properties);
    var result = [];
    try {
        for (var attachPoints_1 = tslib_1.__values(attachPoints), attachPoints_1_1 = attachPoints_1.next(); !attachPoints_1_1.done; attachPoints_1_1 = attachPoints_1.next()) {
            var point = attachPoints_1_1.value;
            var tPoint = MultiArticle.Rule.evalAttachPoint(point, evalConfiguration);
            if (tPoint) {
                result.push(tPoint);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (attachPoints_1_1 && !attachPoints_1_1.done && (_a = attachPoints_1.return)) _a.call(attachPoints_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return result;
};
var mergePointLists = function (currentPoints, newMultiArticlePoints) {
    var e_2, _a, e_3, _b;
    if (currentPoints.length === 0) {
        return newMultiArticlePoints;
    }
    if (newMultiArticlePoints.length === 0) {
        return currentPoints;
    }
    var mergedPoints = new Map();
    try {
        for (var currentPoints_1 = tslib_1.__values(currentPoints), currentPoints_1_1 = currentPoints_1.next(); !currentPoints_1_1.done; currentPoints_1_1 = currentPoints_1.next()) {
            var point = currentPoints_1_1.value;
            mergedPoints.set(point.Key, point);
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (currentPoints_1_1 && !currentPoints_1_1.done && (_a = currentPoints_1.return)) _a.call(currentPoints_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    try {
        for (var newMultiArticlePoints_1 = tslib_1.__values(newMultiArticlePoints), newMultiArticlePoints_1_1 = newMultiArticlePoints_1.next(); !newMultiArticlePoints_1_1.done; newMultiArticlePoints_1_1 = newMultiArticlePoints_1.next()) {
            var point = newMultiArticlePoints_1_1.value;
            mergedPoints.set(point.Key, point);
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (newMultiArticlePoints_1_1 && !newMultiArticlePoints_1_1.done && (_b = newMultiArticlePoints_1.return)) _b.call(newMultiArticlePoints_1);
        }
        finally { if (e_3) throw e_3.error; }
    }
    return Array.from(mergedPoints.values());
};
var ArticleInserter = /** @class */ (function () {
    function ArticleInserter(session, api, loadScheduler, options) {
        this.session = session;
        this.api = api;
        this.loadScheduler = loadScheduler;
        this.options = options;
        this.hooks = {
            /**
             * before a gfx representation is loaded
             */
            prepareForUpdate: new ig_utilities_1.AsyncHook(),
            /**
             * after a gfx representation is loaded and we want to manipulate the gfx representation in the scene
             */
            manipulateAfterUpdate: new ig_utilities_1.AsyncHook(),
            /**
             * after loading and manipulation of loaded articles
             */
            articleUpdateDone: new ig_utilities_1.AsyncHook(),
        };
        this.articleStatus = new Map();
        this.updatePlanningMaterials = function () {
            var e_4, _a;
            var materials = ig_gfx_threejs_1.getAllMaterials();
            try {
                for (var materials_1 = tslib_1.__values(materials), materials_1_1 = materials_1.next(); !materials_1_1.done; materials_1_1 = materials_1.next()) {
                    var material = materials_1_1.value;
                    var mat = material;
                    if (mat.name && mat.name.indexOf("D2PLANNINGMATERIAL") > -1) {
                        mat.transparent = true;
                        if ("map" in mat && mat["map"]) {
                            mat.map.wrapS = mat.map.wrapT = THREE.ClampToEdgeWrapping;
                        }
                    }
                }
            }
            catch (e_4_1) { e_4 = { error: e_4_1 }; }
            finally {
                try {
                    if (materials_1_1 && !materials_1_1.done && (_a = materials_1.return)) _a.call(materials_1);
                }
                finally { if (e_4) throw e_4.error; }
            }
        };
    }
    ArticleInserter.prototype.getArticleLoadingStatus = function (comId) {
        return this.articleStatus.get(comId);
    };
    /**
     * Loads a gfx update for the article
     *
     * @private
     * @param {string} comId
     */
    ArticleInserter.prototype.insertSingleArticle = function (_a) {
        var article = _a.article, configurationOrArticleIdChange = _a.configurationOrArticleIdChange, force = _a.force, animationPositionChange = _a.animationPositionChange;
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var comId_1, configurationHash, isInitial, options, data, articleStatus, gfxRepresentation_1, _b, path, basePath, _c, superRoot, multiArticles, shouldLoadAsPreview, gfxLoadMode, catalogItemId, actionConfiguration, gfxRepresentationSettings, loadIGXCOptions_1, mainComponentId_1, changes, rootOrMainComponentId, appliedPlacement, updateType, result, callbackParams_1, existingGraphicalItem_1, err_1;
            var _this = this;
            return tslib_1.__generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        _d.trys.push([0, 10, , 11]);
                        comId_1 = article.comId;
                        configurationHash = Util_1.createHash(article.configuration);
                        isInitial = article.gfxId === undefined;
                        return [4 /*yield*/, this.hooks.prepareForUpdate.emit(article, this.options.gfxType)];
                    case 1:
                        _d.sent();
                        options = {
                            type: this.options.gfxType,
                        };
                        if (article.gfxId !== undefined) {
                            data = this.api.GetComponentData(article.gfxId);
                            if (data.plannerManager) {
                                if (!this.options.disableHashOptimizations) {
                                    options.visualHash = data.plannerManager.visualHash;
                                    options.geometricHash =
                                        data.plannerManager.geometricHash;
                                    options.pbrHash = data.plannerManager.pbrHash;
                                    if (data.plannerManager.pbrHash !== undefined) {
                                        options.animationPosition = data.animationProgress;
                                    }
                                }
                                if (!configurationOrArticleIdChange && !force) {
                                    if (data.animationProgress === data.plannerManager.animationPosition) {
                                        return [2 /*return*/, { status: "ok" }];
                                    }
                                }
                            }
                            // remove this once the placement is always up to date
                            this.synchronizeArticleSessionPlacement(article.comId);
                        }
                        articleStatus = this.articleStatus.get(comId_1);
                        if (articleStatus !== undefined) {
                            if (configurationHash !== articleStatus.configurationHash) {
                                // article has changed since the preview load -> throw away the current status and use next gfx representation from service
                                // clear hashes so we will always get a full igxc
                                this.articleStatus.delete(comId_1);
                                articleStatus = undefined;
                                options.visualHash = undefined;
                                options.geometricHash = undefined;
                                options.pbrHash = undefined;
                            }
                        }
                        if (!(articleStatus !== undefined)) return [3 /*break*/, 2];
                        _b = articleStatus.gfxRepresentation;
                        return [3 /*break*/, 4];
                    case 2: return [4 /*yield*/, this.session.getGfxRepresentation(comId_1, options)];
                    case 3:
                        _b = _d.sent();
                        _d.label = 4;
                    case 4:
                        gfxRepresentation_1 = _b;
                        if (ig_utilities_1.isNullOrUndefined(gfxRepresentation_1)) {
                            if (this.session.getArticleByComId(comId_1) === undefined) {
                                return [2 /*return*/, {
                                        status: "error",
                                        error: {
                                            type: "articleUnknown",
                                        },
                                    }];
                            }
                            throw new Error("Article \"" + comId_1 + "\" does not have a gfx representation");
                        }
                        if (gfxRepresentation_1 === "UNCHANGED") {
                            return [2 /*return*/, { status: "ok" }];
                        }
                        if (ig_utilities_1.isNullOrUndefined(gfxRepresentation_1.scene)) {
                            throw new Error("Can not load gfxRepresentation for \"" + comId_1 + "\". No scene has been provided");
                        }
                        if (!(typeof gfxRepresentation_1.scene === "string")) return [3 /*break*/, 6];
                        path = gfxRepresentation_1.scene.split("/");
                        basePath = gfxRepresentation_1.scene.substring(0, gfxRepresentation_1.scene.indexOf(path[path.length - 1]));
                        _c = gfxRepresentation_1;
                        return [4 /*yield*/, ig_utilities_1.Http.getJSON(gfxRepresentation_1.scene)];
                    case 5:
                        _c.scene = _d.sent();
                        gfxRepresentation_1.basePath = basePath;
                        _d.label = 6;
                    case 6:
                        if (article.sourcePlacement !== undefined &&
                            article.sourcePlacement.type === "absolute") {
                            gfxRepresentation_1.placement = article.sourcePlacement;
                        }
                        else {
                            gfxRepresentation_1.placement = this.session.getArticlePlacement(comId_1);
                        }
                        superRoot = this.session.getArticleGfxSuperRootId(comId_1);
                        if (superRoot === undefined) {
                            if (gfxRepresentation_1.placement === undefined &&
                                article.sourcePlacement !== undefined &&
                                article.sourcePlacement.type === "absolute") {
                                gfxRepresentation_1.placement = article.sourcePlacement;
                            }
                            superRoot = this.api.CreateSuperRoot();
                        }
                        if (gfxRepresentation_1.placement === undefined) {
                            gfxRepresentation_1.placement = this.session.getArticlePlacement(comId_1);
                        }
                        multiArticles = this.session.getMultiArticlesForComId(article.comId);
                        shouldLoadAsPreview = (this.options.loadMode ===
                            Core.ELoadGeometryPreviews.PreviewOnly ||
                            (articleStatus === undefined &&
                                this.options.loadMode ===
                                    Core.ELoadGeometryPreviews.Staged)) &&
                            this.canLoadAsPreview(gfxRepresentation_1.scene, article.gfxId);
                        gfxLoadMode = this.options.loadMode === undefined
                            ? undefined
                            : shouldLoadAsPreview
                                ? Core.ELoadGeometryPreviews.PreviewOnly
                                : Core.ELoadGeometryPreviews.Default;
                        catalogItemId = article.articleNumber !== undefined
                            ? article.articleNumber.baseArticleNumber
                            : undefined;
                        actionConfiguration = multiArticles
                            ? tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, article.configuration), gfxRepresentation_1.actionConfig), MultiArticleExecutor_1.getMultiArticleConfiguration(multiArticles, article.articleId, catalogItemId)) : article.configuration || {};
                        gfxRepresentationSettings = gfxRepresentation_1.settings || {};
                        loadIGXCOptions_1 = {
                            computeNormals: gfxRepresentationSettings.computeNormals === true,
                            loadMode: gfxLoadMode,
                        };
                        mainComponentId_1 = article.gfxId === undefined ||
                            Core.Hierarchy.isRootGfxId(article.gfxId)
                            ? superRoot
                            : article.gfxId;
                        return [4 /*yield*/, this.loadScheduler
                                .scheduleGfxLoading(function () {
                                return _this.api.ProcessIGXC(mainComponentId_1, 
                                // the scene will always be an igxc.scene, since it's preloaded in the plannermanager
                                gfxRepresentation_1.scene, gfxRepresentation_1.basePath, loadIGXCOptions_1);
                            })
                                .catch(function (err) {
                                _this.articleStatus.delete(comId_1);
                                return Promise.reject(err);
                            })];
                    case 7:
                        changes = _d.sent();
                        rootOrMainComponentId = Core.Hierarchy.fixIfSuperRoot(mainComponentId_1);
                        this.api.SetCommercialId(rootOrMainComponentId, comId_1);
                        if (isInitial) {
                            this.session.setArticleGfxId(article.comId, Core.Hierarchy.getRootGfxId(superRoot));
                        }
                        this.api.RegionManager.updateComponentRegions(rootOrMainComponentId);
                        this.updatePlanningMaterials();
                        appliedPlacement = this.tryApplyPlacement(rootOrMainComponentId, gfxRepresentation_1);
                        if (mainComponentId_1 ===
                            Core.Hierarchy.getSuperRootGfxId(mainComponentId_1)) {
                            this.applyMultiArticleUserData(rootOrMainComponentId, actionConfiguration, multiArticles, article.articleId, catalogItemId);
                            this.setChildSelectionMode(rootOrMainComponentId);
                            this.prepareNiches(rootOrMainComponentId, multiArticles, actionConfiguration);
                        }
                        this.prepareRoot(rootOrMainComponentId);
                        if (!appliedPlacement) {
                            this.executeDefaultPlacement(comId_1, rootOrMainComponentId);
                        }
                        updateType = changes === Core.ProcessIGXCChanges.Structural
                            ? "geometric"
                            : "materialOnly";
                        result = void 0;
                        if (shouldLoadAsPreview &&
                            this.options.loadMode === Core.ELoadGeometryPreviews.Staged) {
                            this.articleStatus.set(comId_1, {
                                status: "preview",
                                gfxRepresentation: gfxRepresentation_1,
                                configurationHash: configurationHash,
                            });
                            result = {
                                status: "updateRequired",
                            };
                        }
                        else if (articleStatus !== undefined) {
                            this.articleStatus.delete(comId_1);
                        }
                        this.synchronizeArticleSessionPlacement(article.comId);
                        callbackParams_1 = {
                            initial: isInitial,
                            status: gfxLoadMode !== Core.ELoadGeometryPreviews.PreviewOnly
                                ? "final"
                                : "preview",
                            updateType: updateType,
                        };
                        existingGraphicalItem_1 = this.session.getArticleByComId(article.comId);
                        this.applyComponentData(existingGraphicalItem_1.gfxId, {
                            geometricHash: gfxRepresentation_1.geometricHash,
                            visualHash: gfxRepresentation_1.visualHash,
                            pbrHash: gfxRepresentation_1.pbrHash,
                            animationPosition: options.animationPosition,
                        });
                        return [4 /*yield*/, this.loadScheduler
                                .schedulePostUpdateManipulation(function () {
                                return _this.hooks.manipulateAfterUpdate.emit(existingGraphicalItem_1, callbackParams_1);
                            })
                                .catch(function (err) {
                                return Logger.error("Encountered an error inside postUpdateManipulation hooks", err);
                            })];
                    case 8:
                        _d.sent();
                        return [4 /*yield*/, this.hooks.articleUpdateDone
                                .emit(existingGraphicalItem_1, callbackParams_1)
                                .catch(function (err) {
                                return Logger.error("Encountered an error inside articleUpdateDone hooks", err);
                            })];
                    case 9:
                        _d.sent();
                        return [2 /*return*/, result || { status: "ok" }];
                    case 10:
                        err_1 = _d.sent();
                        return [2 /*return*/, {
                                status: "error",
                                error: {
                                    type: "unknown",
                                    error: err_1,
                                },
                            }];
                    case 11: return [2 /*return*/];
                }
            });
        });
    };
    ArticleInserter.prototype.synchronizeArticleSessionPlacement = function (comId) {
        var placement = this.getSceneArticlePlacement(comId);
        if (placement !== undefined) {
            this.session.setArticlePlacement(comId, placement);
        }
    };
    ArticleInserter.prototype.canLoadAsPreview = function (igxc, gfxId) {
        return this.api.IsPreviewIGXCLoadPossibleAndNeccessary(igxc, gfxId);
    };
    ArticleInserter.prototype.getSceneArticlePlacement = function (comId) {
        var gfxId = this.session.getArticleGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        var component = this.api.GetComponent(gfxId);
        if (ig_utilities_1.isNullOrUndefined(component)) {
            return undefined;
        }
        return {
            position: this.api.GetPosition(gfxId),
            rotation: {
                x: 0,
                y: this.api.GetYRotation(gfxId),
                z: 0,
            },
        };
    };
    ArticleInserter.prototype.applyComponentData = function (gfxId, data) {
        this.api.SetComponentData(gfxId, "plannerManager", data);
    };
    ArticleInserter.prototype.applyMultiArticleUserData = function (rootId, properties, multiArticles, articleId, catalogItemId) {
        this.api.SetComponentData(rootId, "actions", multiArticles !== undefined
            ? MultiArticleExecutor_1.getMultiArticleActions(multiArticles, articleId, catalogItemId)
            : []);
        // Add categories:
        if (multiArticles !== undefined) {
            var multiArticleCategories = MultiArticleExecutor_1.getMultiArticleCategories(multiArticles, articleId, catalogItemId);
            if (multiArticleCategories) {
                var currentCategories = this.api
                    .GetCategories(rootId)
                    .slice(0);
                for (var tCategory in multiArticleCategories) {
                    if (multiArticleCategories.hasOwnProperty(tCategory)) {
                        if (MultiArticle.Rule.evalBooleanRule(multiArticleCategories[tCategory], properties)) {
                            currentCategories.push(tCategory);
                        }
                    }
                }
                this.api.SetCategories(rootId, currentCategories);
                var multiArticleConfig = MultiArticleExecutor_1.getMultiArticleConfiguration(multiArticles, articleId, catalogItemId);
                this.api.SetComponentData(rootId, "multiArticleConfiguration", multiArticleConfig);
            }
            // Add attach points:
            var multiArticlePoints = MultiArticleExecutor_1.getMultiArticleAttachPoints(multiArticles, articleId, catalogItemId);
            var points = this.api.GetIGXCPoints(rootId) || [];
            if (multiArticlePoints !== undefined) {
                var evaluatedAttachPoints = evaluateMultiArticlePoints(multiArticlePoints, rootId, properties);
                points = mergePointLists(points, evaluatedAttachPoints);
            }
            this.api.SetIGXCPoints(rootId, points);
        }
        else {
            // No multi articles -> add LeftRightChain category:
            this.api.SetCategories(rootId, [Core.Category.LeftRightChain]);
        }
    };
    ArticleInserter.prototype.setChildSelectionMode = function (rootId) {
        var _this = this;
        this.api.TraverseComponent(rootId, function (componentId) {
            _this.api.SetSelectionMode(componentId, "Parent");
        });
    };
    ArticleInserter.prototype.prepareRoot = function (rootId) {
        if (Core.Category.hasCategory(rootId, Core.Category.NicheContainer)) {
            this.api.SetVisibility(rootId, false);
        }
    };
    ArticleInserter.prototype.prepareNiches = function (rootId, multiArticles, configuration) {
        var e_5, _a, e_6, _b, e_7, _c;
        var _this = this;
        var nicheIds = [];
        this.api.TraverseComponent(rootId, function (componentId) {
            var userData = _this.api.GetComponentData(componentId);
            if (userData &&
                userData.layer &&
                MultiArticle.Niche.nicheRegex.test(userData.layer)) {
                nicheIds.push(componentId);
            }
        });
        if (multiArticles === undefined) {
            try {
                for (var nicheIds_1 = tslib_1.__values(nicheIds), nicheIds_1_1 = nicheIds_1.next(); !nicheIds_1_1.done; nicheIds_1_1 = nicheIds_1.next()) {
                    var componentId = nicheIds_1_1.value;
                    this.api.SetVisibility(componentId, false);
                }
            }
            catch (e_5_1) { e_5 = { error: e_5_1 }; }
            finally {
                try {
                    if (nicheIds_1_1 && !nicheIds_1_1.done && (_a = nicheIds_1.return)) _a.call(nicheIds_1);
                }
                finally { if (e_5) throw e_5.error; }
            }
            return;
        }
        var niches = nicheIds.map(function (nicheId) {
            var layer = _this.api.GetLayer(nicheId);
            var _a = tslib_1.__read(MultiArticle.Niche.nicheRegex.exec(layer), 2), nicheIdentifier = _a[1];
            var newNiche = new MultiArticle.Niche(nicheId, nicheIdentifier, rootId);
            return newNiche;
        });
        niches.forEach(function (niche) {
            var nicheActions = multiArticles.getActions(niche.multiArticlecId());
            _this.api.SetComponentData(niche.getComponentId(), "actions", nicheActions);
        });
        var rootConverter = new Planning.WorldLocalConverter(rootId);
        var nichePoints = [];
        var _loop_1 = function (niche) {
            // convert from niche coordinates to world coordinates
            var nicheId = niche.getComponentId();
            var oldScaling = this_1.api.GetScaling(nicheId);
            this_1.api.SetScaling(nicheId, { x: 1, y: 1, z: 1 });
            var nicheConverter = new Planning.WorldLocalConverter(nicheId);
            var BBOX = MultiArticle.Rule.createBoundingBoxForRule(nicheId);
            // BUG: Some race conditions (?) may prevent niche to be loaded
            // in this case, just return nothing
            var tIsBboxInvalid = [
                BBOX.local.min.x,
                BBOX.local.min.y,
                BBOX.local.min.z,
            ].some(function (num) { return isNaN(num) || !isFinite(num); });
            if (tIsBboxInvalid) {
                Logger.error("There was a problem evaluating the BBOX: " + JSON.stringify(BBOX) + " for the niche " + nicheId);
                this_1.api.SetScaling(nicheId, oldScaling);
                return "continue";
            }
            var nicheAttachPoints = niche.attachPoints(multiArticles, tslib_1.__assign({ BBOX: BBOX }, configuration));
            var nicheAttachPointsInLocalCoordinates = nicheAttachPoints.map(function (pt) {
                var inWorldCoordinates = nicheConverter.toWorld({
                    x: pt.Point.X,
                    y: pt.Point.Y,
                    z: pt.Point.Z,
                });
                var inRootCoordinates = rootConverter.toLocal(inWorldCoordinates);
                pt.Point = {
                    X: inRootCoordinates.x,
                    Y: inRootCoordinates.y,
                    Z: inRootCoordinates.z,
                };
                pt.ComponentId = niche.getComponentId();
                return pt;
            });
            this_1.api.SetScaling(nicheId, oldScaling);
            nichePoints.push.apply(nichePoints, tslib_1.__spread(nicheAttachPointsInLocalCoordinates));
        };
        var this_1 = this;
        try {
            for (var niches_1 = tslib_1.__values(niches), niches_1_1 = niches_1.next(); !niches_1_1.done; niches_1_1 = niches_1.next()) {
                var niche = niches_1_1.value;
                _loop_1(niche);
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (niches_1_1 && !niches_1_1.done && (_b = niches_1.return)) _b.call(niches_1);
            }
            finally { if (e_6) throw e_6.error; }
        }
        var oldPoints = this.api.GetIGXCPoints(rootId);
        this.api.SetIGXCPoints(rootId, mergePointLists(oldPoints, nichePoints));
        try {
            for (var nicheIds_2 = tslib_1.__values(nicheIds), nicheIds_2_1 = nicheIds_2.next(); !nicheIds_2_1.done; nicheIds_2_1 = nicheIds_2.next()) {
                var componentId = nicheIds_2_1.value;
                this.api.SetVisibility(componentId, false);
            }
        }
        catch (e_7_1) { e_7 = { error: e_7_1 }; }
        finally {
            try {
                if (nicheIds_2_1 && !nicheIds_2_1.done && (_c = nicheIds_2.return)) _c.call(nicheIds_2);
            }
            finally { if (e_7) throw e_7.error; }
        }
    };
    ArticleInserter.prototype.tryApplyPlacement = function (componentId, gfxRepresentation) {
        var placedArticle = false;
        var position = new Core.Vector3(0, 0, 0);
        var rotation = new Core.Vector3(0, 0, 0);
        if (gfxRepresentation.placement &&
            gfxRepresentation.placement.position) {
            position = gfxRepresentation.placement.position;
            placedArticle = true;
        }
        else {
            position = this.api.GetPosition(componentId);
        }
        if (gfxRepresentation.placement &&
            gfxRepresentation.placement.rotation) {
            rotation.x = Core.MathUtils.degToRad(gfxRepresentation.placement.rotation.x);
            rotation.y = Core.MathUtils.degToRad(gfxRepresentation.placement.rotation.y);
            rotation.z = Core.MathUtils.degToRad(gfxRepresentation.placement.rotation.z);
            placedArticle = true;
        }
        this.api.SetTransformation(componentId, Core.createTransformation(position, rotation));
        return placedArticle;
    };
    ArticleInserter.prototype.executeDefaultPlacement = function (comId, gfxId) {
        if (this.options.executeArticlePlacement !== undefined) {
            this.options.executeArticlePlacement(this.api, comId, gfxId);
            return;
        }
        var decider = new Planning.PlacementDecision(gfxId, this.api.GetAllSelectedComponents());
        var placementDecision = decider.getTransformation();
        this.api.SetTransformation(gfxId, placementDecision);
    };
    return ArticleInserter;
}());
exports.ArticleInserter = ArticleInserter;
