"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ProcessFurtherOperation_1 = require("./ProcessFurtherOperation");
/** Class for adapting transformations to a grid. Processes a TranslateInPlaneOperation or similar */
var GridTranslationOperation = /** @class */ (function (_super) {
    tslib_1.__extends(GridTranslationOperation, _super);
    function GridTranslationOperation(preceedingOperation, gridLength) {
        var _this = _super.call(this, preceedingOperation) || this;
        _this.gridLength = gridLength;
        return _this;
    }
    GridTranslationOperation.prototype.processMotionResult = function (resultParam) {
        var _this = this;
        var result = tslib_1.__assign(tslib_1.__assign({}, resultParam), { transformations: resultParam.transformations.map(function (trafo) {
                return _this.adaptToGrid(trafo);
            }) });
        return result;
    };
    GridTranslationOperation.prototype.processEndResult = function (resultParam) {
        var _this = this;
        var result = tslib_1.__assign(tslib_1.__assign({}, resultParam), { transformations: resultParam.transformations.map(function (trafo) {
                return _this.adaptToGrid(trafo);
            }) });
        return result;
    };
    /**
     * Adaption only applies to x and z component. Rotation is not considered here, see GridRotationOperation for this behaviour
     * @param transformation
     * @returns {Core.Transformation}
     */
    GridTranslationOperation.prototype.adaptToGrid = function (transformation) {
        return tslib_1.__assign(tslib_1.__assign({}, transformation), { position: {
                x: this.closestOnGrid(transformation.position.x),
                y: transformation.position.y,
                z: this.closestOnGrid(transformation.position.z),
            } });
    };
    /**
     * Returns the closest point on the grid for the given scalar x
     * @param x
     * @returns {number}
     */
    GridTranslationOperation.prototype.closestOnGrid = function (x) {
        return Math.round(x / this.gridLength) * this.gridLength;
    };
    return GridTranslationOperation;
}(ProcessFurtherOperation_1.ProcessFurtherOperation));
exports.GridTranslationOperation = GridTranslationOperation;
