"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Core = require("@intelligentgraphics/ig.gfx.core");
exports.placeComponentAtAttachPoint = function (api, parentGfxId, childGfxId, parentAttachPoint, childAttachPoint) {
    // 1) calculate real corpus snappoint in world coordinates
    // calculate snappoint position if corpus rotation would be 0
    var parentPosition = api.GetPosition(parentGfxId);
    var parentRotation = api.GetYRotation(parentGfxId);
    var tNonRotatedSnappointAtCorpusOrigin = Core.Vector.Add3(parentPosition, parentAttachPoint.point);
    // calculate real corpus snappoint position by rotating the above position around corpus origin
    var tCorpusAttachReal = Core.LinearAlgebra.AdaptToRotation(Core.MathUtils.degToRad(parentRotation), tNonRotatedSnappointAtCorpusOrigin, parentPosition);
    // 2) calculate real article snappoint in world coordinates
    // calculate snappoint position if article rotation would be 0
    var tArticlePosition = api.GetPosition(childGfxId);
    var tArticleRotation = api.GetYRotation(childGfxId);
    var tNonRotatedSnappointAtArticleOrigin = Core.Vector.Add3(tArticlePosition, childAttachPoint.point);
    // calculate real article snappoint position by rotating the above position around article origin
    var tArticleAttachReal = Core.LinearAlgebra.AdaptToRotation(Core.MathUtils.degToRad(tArticleRotation), tNonRotatedSnappointAtArticleOrigin, tArticlePosition);
    // 3) move article so the snappoints are at the same position, however the rotations do not match yet
    var tPointDiff = Core.Vector.Sub3(tCorpusAttachReal, tArticleAttachReal); // A-> C
    var tNewArticlePos = Core.Vector3.add(tArticlePosition, tPointDiff);
    api.SetPosition(childGfxId, tNewArticlePos);
    // 4) calculate final needed article rotation
    //const tFinalArticleRotationDeg =
    //	parentRotation -
    //	(childAttachPoint.rotation.y - parentAttachPoint.rotation.y);
    var tFinalArticleRotationDeg = tArticleRotation + //already // difference still needed
        (parentRotation +
            parentAttachPoint.rotation.y - // full parent
            (tArticleRotation + childAttachPoint.rotation.y)); // full article
    // 5) rotate article around snappoint (not around its origin !!!)
    var tRotationDifferenceDeg = -(tArticleRotation - tFinalArticleRotationDeg);
    var tFinalPosition = Core.LinearAlgebra.AdaptToRotation(Core.MathUtils.degToRad(tRotationDifferenceDeg), tNewArticlePos, tCorpusAttachReal);
    api.SetPosition(childGfxId, tFinalPosition);
    api.SetYRotation(childGfxId, tFinalArticleRotationDeg);
};
exports.createHash = function (input) {
    var e_1, _a;
    var keys = Object.getOwnPropertyNames(input).sort();
    var hash = "";
    try {
        for (var keys_1 = tslib_1.__values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
            var key = keys_1_1.value;
            hash += key + "=" + input[key] + ";;;;";
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return hash;
};
