import * as Core from "@intelligentgraphics/ig.gfx.core";
export const placeComponentAtAttachPoint = (api, parentGfxId, childGfxId, parentAttachPoint, childAttachPoint) => {
    // 1) calculate real corpus snappoint in world coordinates
    // calculate snappoint position if corpus rotation would be 0
    const parentPosition = api.GetPosition(parentGfxId);
    const parentRotation = api.GetYRotation(parentGfxId);
    const tNonRotatedSnappointAtCorpusOrigin = Core.Vector.Add3(parentPosition, parentAttachPoint.point);
    // calculate real corpus snappoint position by rotating the above position around corpus origin
    const 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
    const tArticlePosition = api.GetPosition(childGfxId);
    const tArticleRotation = api.GetYRotation(childGfxId);
    const tNonRotatedSnappointAtArticleOrigin = Core.Vector.Add3(tArticlePosition, childAttachPoint.point);
    // calculate real article snappoint position by rotating the above position around article origin
    const 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
    const tPointDiff = Core.Vector.Sub3(tCorpusAttachReal, tArticleAttachReal); // A-> C
    const 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);
    const 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 !!!)
    const tRotationDifferenceDeg = -(tArticleRotation - tFinalArticleRotationDeg);
    const tFinalPosition = Core.LinearAlgebra.AdaptToRotation(Core.MathUtils.degToRad(tRotationDifferenceDeg), tNewArticlePos, tCorpusAttachReal);
    api.SetPosition(childGfxId, tFinalPosition);
    api.SetYRotation(childGfxId, tFinalArticleRotationDeg);
};
export const createHash = (input) => {
    const keys = Object.getOwnPropertyNames(input).sort();
    let hash = "";
    for (const key of keys) {
        hash += `${key}=${input[key]};;;;`;
    }
    return hash;
};
