"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @hidden
 */
var ColumnResize = /** @class */ (function () {
    function ColumnResize(triggerResize) {
        var _this = this;
        /**
         * The settings for resizing the Grid.
         */
        this.resizable = false;
        this.isRtl = false;
        this.setIsRtl = function (isRtl) {
            _this.isRtl = isRtl;
        };
        this.onResize = triggerResize;
        this.dragHandler = this.dragHandler.bind(this);
    }
    ColumnResize.prototype.dragHandler = function (event, column, dragCue, end) {
        var e = event.originalEvent;
        if (!end) {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();
        }
        var tdElement = dragCue.parentElement;
        if (!tdElement || !tdElement.parentElement) {
            return;
        }
        var oldWidth = tdElement.clientWidth;
        var newWidth;
        if (this.isRtl) {
            newWidth = (dragCue.getBoundingClientRect().right - (dragCue.offsetWidth / 2)) - event.clientX;
            newWidth += oldWidth;
        }
        else {
            newWidth = oldWidth + event.clientX - dragCue.getBoundingClientRect().left - (dragCue.offsetWidth / 2);
        }
        if (!end && Math.abs(newWidth - oldWidth) < 1) {
            return;
        }
        this.setWidths(column, Math.floor(newWidth) / oldWidth);
        var index = this.columns.filter(function (c) { return !c.children.length; }).indexOf(column);
        this.onResize(index, oldWidth, newWidth, e, end);
    };
    ColumnResize.prototype.setWidths = function (column, coef) {
        var indexInOriginal = this.columns.indexOf(column);
        var toAdjust = [];
        var more = column.children.length;
        for (var i = indexInOriginal + 1; more > 0 && i < this.columns.length; i++, more--) {
            var cc = this.columns[i];
            if (!cc.children.length) {
                toAdjust.push(cc);
            }
            else {
                more += cc.children.length;
            }
        }
        if (toAdjust.length === 0) {
            toAdjust.push(column);
        }
        toAdjust.forEach(function (colToAdjust) {
            var targetWidth = colToAdjust.width ? parseFloat(colToAdjust.width.toString()) * coef : 0;
            var min = colToAdjust.minResizableWidth === undefined ? 10 : colToAdjust.minResizableWidth;
            if (targetWidth < min) {
                targetWidth = min;
            }
            colToAdjust.width = Math.floor(targetWidth);
        });
        this.updateColElements(toAdjust);
    };
    ColumnResize.prototype.updateColElements = function (affectedColumns) {
        var columns = this.columns.filter(function (c) { return !c.children.length; });
        var difference = 1e-10;
        for (var i = 0; i < affectedColumns.length; i++) {
            var colIndex = columns.indexOf(affectedColumns[i]);
            var currentColumnFloatWidth = parseFloat((affectedColumns[i].width || 0).toString());
            difference += currentColumnFloatWidth - Math.floor(currentColumnFloatWidth);
            var currentWidth = Math.floor(currentColumnFloatWidth) + Math.floor(difference);
            difference -= Math.floor(difference);
            this.setColWidth(this.colGroupMain && this.colGroupMain.children[colIndex], currentWidth);
            this.setColWidth(this.colGroupHeader && this.colGroupHeader.children[colIndex], currentWidth);
            this.setColWidth(this.colGroupFooter && this.colGroupFooter.children[colIndex], currentWidth);
        }
    };
    ColumnResize.prototype.setColWidth = function (col, width) {
        if (col) {
            col.style.width = width + 'px';
        }
    };
    return ColumnResize;
}());
exports.ColumnResize = ColumnResize;
//# sourceMappingURL=ColumnResize.js.map