import { TableWidget, TableCellWidget } from '../index';
import { isNullOrUndefined } from '@syncfusion/ej2-base';
import { Point, HelperMethods } from './editor-helper';
/**
 * @private
 */
var TableResizer = /** @class */ (function () {
    function TableResizer(node) {
        this.resizeNode = 0;
        this.resizerPosition = -1;
        this.currentResizingTable = undefined;
        this.isResizerEnabled = false;
        this.owner = node;
        this.documentHelper = this.owner.documentHelper;
        this.startingPoint = new Point(0, 0);
    }
    Object.defineProperty(TableResizer.prototype, "viewer", {
        get: function () {
            return this.owner.viewer;
        },
        enumerable: true,
        configurable: true
    });
    TableResizer.prototype.getModuleName = function () {
        return 'TableResizer';
    };
    TableResizer.prototype.updateResizingHistory = function (touchPoint) {
        if (this.owner.editorHistoryModule) {
            this.owner.editorHistoryModule.updateResizingHistory(touchPoint, this);
        }
        this.documentHelper.isRowOrCellResizing = false;
        this.owner.isShiftingEnabled = false;
        this.resizerPosition = -1;
    };
    TableResizer.prototype.handleResize = function (point) {
        this.owner.documentHelper.isRowOrCellResizing = true;
        this.startingPoint.x = point.x;
        this.startingPoint.y = point.y;
        //Initialize resizing history.
        this.owner.editorHistoryModule.initResizingHistory(point, this);
    };
    //Table Resizing implementation starts
    TableResizer.prototype.isInRowResizerArea = function (touchPoint) {
        this.documentHelper.resizerBoundaryWidth = 3 / this.documentHelper.zoomFactor;
        var position = this.getRowReSizerPosition(undefined, touchPoint);
        this.documentHelper.resizerBoundaryWidth = 0;
        if (position === -1) {
            return false;
        }
        else {
            this.resizeNode = 1;
            this.resizerPosition = position;
            return true;
        }
    };
    TableResizer.prototype.isInCellResizerArea = function (touchPoint) {
        this.documentHelper.resizerBoundaryWidth = 3 / this.documentHelper.zoomFactor;
        var position = this.getCellReSizerPosition(touchPoint);
        if (position === -1) {
            this.isResizerEnabled = false;
            return false;
        }
        else {
            this.resizeNode = 0;
            this.isResizerEnabled = true;
            this.resizerPosition = position;
            return true;
        }
    };
    TableResizer.prototype.getCellReSizerPosition = function (touchPoint) {
        var position = -1;
        var resizerBoundaryWidth = 2;
        var tableWidget = this.getTableWidget(touchPoint);
        var cellWidget = this.getTableCellWidget(touchPoint);
        var cellSpacing = isNullOrUndefined(tableWidget) ? 0 : tableWidget.tableFormat.cellSpacing;
        if (tableWidget && cellSpacing > 0) {
            this.currentResizingTable = tableWidget;
            /* eslint-disable-next-line max-len */
            if (this.documentHelper.isInsideRect(tableWidget.x - HelperMethods.convertPointToPixel(tableWidget.leftBorderWidth) - 0.25, tableWidget.y, HelperMethods.convertPointToPixel(tableWidget.leftBorderWidth) + 0.5, tableWidget.height, touchPoint)) {
                return position = 0;
            }
            var startingPointX = tableWidget.x;
            for (var i = 0; i < tableWidget.tableHolder.columns.length; i++) {
                var preferredWidth = HelperMethods.convertPointToPixel(tableWidget.tableHolder.columns[parseInt(i.toString(), 10)].preferredWidth);
                /* eslint-disable-next-line max-len */
                if ((this.documentHelper.isInsideRect(startingPointX - 1, tableWidget.y, tableWidget.leftBorderWidth + resizerBoundaryWidth, tableWidget.height, touchPoint))) {
                    return position = i > 0 ? i : 0;
                    /* eslint-disable-next-line max-len */
                }
                else if (i > 0 && (this.documentHelper.isInsideRect(startingPointX + preferredWidth - resizerBoundaryWidth / 2, tableWidget.y, resizerBoundaryWidth, tableWidget.height, touchPoint))) {
                    return position = (i + 1);
                }
                startingPointX = startingPointX + preferredWidth;
            }
        }
        else {
            if (!isNullOrUndefined(cellWidget)) {
                this.currentResizingTable = cellWidget.ownerTable;
                /* eslint-disable-next-line max-len */
                if (this.documentHelper.isInsideRect(cellWidget.x - cellWidget.margin.left - resizerBoundaryWidth / 2, cellWidget.y - cellWidget.margin.top, resizerBoundaryWidth, cellWidget.height + cellWidget.margin.top + cellWidget.margin.bottom, touchPoint)) {
                    return position = this.currentResizingTable.isBidiTable ? (cellWidget.columnIndex + cellWidget.cellFormat.columnSpan) :
                        cellWidget.columnIndex;
                }
                else if ((isNullOrUndefined(cellWidget.nextRenderedWidget) || this.currentResizingTable.isBidiTable)
                    /* eslint-disable-next-line max-len */
                    && this.documentHelper.isInsideRect(cellWidget.x + cellWidget.margin.right + cellWidget.width - resizerBoundaryWidth / 2, cellWidget.y - cellWidget.margin.top, resizerBoundaryWidth, cellWidget.height + cellWidget.margin.top + cellWidget.margin.bottom, touchPoint)) {
                    return position = this.currentResizingTable.isBidiTable ? cellWidget.columnIndex :
                        (cellWidget.columnIndex + cellWidget.cellFormat.columnSpan);
                }
                else if (cellWidget.childWidgets.length > 0) {
                    return this.getCellReSizerPositionInternal(cellWidget, touchPoint); // Gets the nested table resizer position.
                }
            }
        }
        return position;
    };
    TableResizer.prototype.getCellReSizerPositionInternal = function (cellWidget, touchPoint) {
        var position = -1;
        var childTableWidget = this.getTableWidgetFromWidget(touchPoint, cellWidget);
        var childCellWidget = undefined;
        if (!isNullOrUndefined(childTableWidget) && childTableWidget.tableFormat.cellSpacing > 0) {
            this.currentResizingTable = childTableWidget;
            /* eslint-disable-next-line max-len */
            if (this.documentHelper.isInsideRect(childTableWidget.x - childTableWidget.leftBorderWidth - 0.25, childTableWidget.y, childTableWidget.leftBorderWidth + 0.5, childTableWidget.height, touchPoint)) {
                return position = 0;
            }
            var startingPointX = childTableWidget.x;
            for (var i = 0; i < childTableWidget.tableHolder.columns.length; i++) {
                var preferredWidth = HelperMethods.convertPointToPixel(childTableWidget.tableHolder.columns[parseInt(i.toString(), 10)].preferredWidth);
                /* eslint-disable-next-line max-len */
                if ((this.documentHelper.isInsideRect(startingPointX - 1, childTableWidget.y, childTableWidget.leftBorderWidth + 2, childTableWidget.height, touchPoint))) {
                    return position = i > 0 ? i : 0;
                    /* eslint-disable-next-line max-len */
                }
                else if (i > 0 && (this.documentHelper.isInsideRect(startingPointX + preferredWidth - 1, childTableWidget.y, 2, childTableWidget.height, touchPoint))) {
                    return position = (i + 1);
                }
                startingPointX = startingPointX + preferredWidth;
            }
        }
        else {
            if (!isNullOrUndefined(childTableWidget)) {
                childCellWidget = childTableWidget.getTableCellWidget(touchPoint);
            }
            if (!isNullOrUndefined(childCellWidget)) {
                this.currentResizingTable = childCellWidget.ownerTable;
                /* eslint-disable-next-line max-len */
                if (this.documentHelper.isInsideRect(childCellWidget.x - childCellWidget.margin.left - 1, childCellWidget.y - childCellWidget.margin.top, 2, childCellWidget.height, touchPoint)) {
                    return position = childCellWidget.columnIndex;
                }
                else if (isNullOrUndefined(childCellWidget.nextRenderedWidget)
                    /* eslint-disable-next-line max-len */
                    && this.documentHelper.isInsideRect(childCellWidget.x + childCellWidget.margin.right + childCellWidget.width - 1, childCellWidget.y - childCellWidget.margin.top, 2, childCellWidget.height, touchPoint)) {
                    return position = (childCellWidget.columnIndex + childCellWidget.cellFormat.columnSpan);
                }
                else if (childCellWidget.childWidgets.length > 0) {
                    return this.getCellReSizerPositionInternal(childCellWidget, touchPoint);
                }
            }
            else if (this.isResizerEnabled && this.owner.selection.getSelectedCells().length === 0 && this.owner.documentHelper.isMouseDown) {
                position = this.resizerPosition;
            }
        }
        return position;
    };
    TableResizer.prototype.getRowReSizerPosition = function (widget, touchPoint) {
        var tableWidget = undefined;
        var cellWidget = undefined;
        if (isNullOrUndefined(widget)) {
            tableWidget = this.getTableWidget(touchPoint);
            cellWidget = this.getTableCellWidget(touchPoint);
        }
        else {
            tableWidget = this.getTableWidgetFromWidget(touchPoint, widget);
        }
        var cellSpacing = isNullOrUndefined(tableWidget) ? 0 : tableWidget.tableFormat.cellSpacing;
        if (tableWidget && cellSpacing > 0) {
            this.currentResizingTable = tableWidget;
            /* eslint-disable-next-line max-len */
            if (this.owner.documentHelper.isInsideRect(tableWidget.x, tableWidget.y + tableWidget.height - cellSpacing, this.getActualWidth(tableWidget.lastChild), (isNullOrUndefined(tableWidget.nextSplitWidget) ? tableWidget.bottomBorderWidth + cellSpacing : 0), touchPoint)) {
                return tableWidget.lastChild.rowIndex;
            }
            for (var i = 0; i < tableWidget.childWidgets.length; i++) {
                //Need to consider for splitted widgets
                var rowWidget = tableWidget.childWidgets[parseInt(i.toString(), 10)];
                if (tableWidget.childWidgets.indexOf(rowWidget) > -1
                    /* eslint-disable-next-line max-len */
                    && (this.owner.documentHelper.isInsideRect(rowWidget.x, rowWidget.y + rowWidget.height + cellSpacing / 2, this.getActualWidth(rowWidget), cellSpacing / 2, touchPoint))) {
                    return rowWidget.rowIndex;
                }
            }
        }
        else {
            if (tableWidget && cellWidget) {
                cellWidget = this.getTableCellWidget(touchPoint);
            }
            if (cellWidget) {
                var rowWidget = cellWidget.containerWidget;
                var height = 0;
                var rowHeight = 0;
                if (rowWidget.rowIndex === rowWidget.ownerTable.childWidgets.length - 1) {
                    height = rowWidget.bottomBorderWidth + this.documentHelper.resizerBoundaryWidth;
                    rowHeight = rowWidget.y + rowWidget.height - height;
                }
                else {
                    height = rowWidget.nextRenderedWidget.topBorderWidth + this.documentHelper.resizerBoundaryWidth;
                    rowHeight = rowWidget.y + rowWidget.height;
                }
                /* eslint-disable-next-line max-len */
                if (this.owner.documentHelper.isInsideRect(rowWidget.x, rowHeight, rowWidget.width, height, touchPoint)) {
                    this.currentResizingTable = rowWidget.ownerTable;
                    return rowWidget.rowIndex;
                }
                else {
                    if (cellWidget.childWidgets.length > 0) {
                        return this.getRowReSizerPosition(cellWidget, touchPoint);
                    }
                }
            }
        }
        return -1;
    };
    TableResizer.prototype.handleResizing = function (touchPoint, isTableMarkerDragging, dragValue) {
        this.owner.isShiftingEnabled = true;
        if (!isTableMarkerDragging) {
            if (this.resizeNode === 0) {
                dragValue = HelperMethods.round(touchPoint.x - this.startingPoint.x, 2);
                this.resizeTableCellColumn(dragValue);
            }
            else {
                dragValue = touchPoint.y - this.startingPoint.y;
            }
        }
        else {
            if (this.resizeNode === 0) {
                this.resizeTableCellColumn(dragValue);
            }
        }
    };
    TableResizer.prototype.resizeTableRow = function (dragValue) {
        var table = this.currentResizingTable;
        if (isNullOrUndefined(table) || dragValue === 0 || this.resizerPosition === -1) {
            return;
        }
        var selection = this.owner.selectionModule;
        if (table.isInsideTable) {
            this.owner.isLayoutEnabled = false; //Layouting is disabled to skip the child table layouting.
        }
        var row = undefined;
        if (this.resizerPosition > -1) {
            row = table.childWidgets[this.resizerPosition];
            if (row) {
                this.updateRowHeight(row, dragValue);
            }
            selection.selectPosition(selection.start, selection.end);
        }
        if (table.isInsideTable) {
            var parentTable = this.owner.documentHelper.layout.getParentTable(table);
            this.owner.isLayoutEnabled = true; //layouting is enabled to layout the parent table of the nested table.
            table = parentTable;
        }
        if (!isNullOrUndefined(row)) {
            if (row.rowFormat.heightType === 'Exactly') {
                this.startingPoint.y += HelperMethods.convertPointToPixel(dragValue);
            }
            else {
                this.startingPoint.y += dragValue;
            }
        }
        this.owner.documentHelper.layout.reLayoutTable(table);
        this.owner.editorModule.isSkipOperationsBuild = this.owner.enableCollaborativeEditing;
        this.owner.editorModule.reLayout(this.owner.selectionModule);
        this.owner.editorModule.isSkipOperationsBuild = false;
        // if (row) {
        //     this.getRowReSizerPosition(undefined, this.startingPoint);
        // }
        if (this.currentResizingTable && (this.currentResizingTable.childWidgets === undefined
            || this.currentResizingTable.childWidgets[this.resizerPosition] === undefined)) {
            this.resizerPosition = -1;
        }
    };
    TableResizer.prototype.getTableWidget = function (cursorPoint) {
        var widget = undefined;
        var currentPage = this.owner.documentHelper.currentPage;
        if (!isNullOrUndefined(currentPage)) {
            for (var i = 0; i < currentPage.bodyWidgets.length; i++) {
                var bodyWidget = currentPage.bodyWidgets[parseInt(i.toString(), 10)];
                widget = this.getTableWidgetFromWidget(cursorPoint, bodyWidget);
                if (!isNullOrUndefined(widget)) {
                    break;
                }
                if (this.documentHelper.owner.layoutType === 'Pages' &&
                    !(isNullOrUndefined(currentPage.headerWidget) && isNullOrUndefined(currentPage.footerWidget))) {
                    var body = currentPage.headerWidget;
                    widget = this.getTableWidgetFromWidget(cursorPoint, body);
                    if (!isNullOrUndefined(widget)) {
                        break;
                    }
                    var bodywid = currentPage.footerWidget;
                    widget = this.getTableWidgetFromWidget(cursorPoint, bodywid);
                    if (!isNullOrUndefined(widget)) {
                        break;
                    }
                }
            }
        }
        return widget;
    };
    TableResizer.prototype.getTableWidgetFromWidget = function (point, widget) {
        for (var j = 0; j < widget.childWidgets.length; j++) {
            if (widget.childWidgets[parseInt(j.toString(), 10)] instanceof TableWidget) {
                var childWidget = widget.childWidgets[parseInt(j.toString(), 10)];
                var tableWidth = 0;
                if (childWidget.wrapTextAround) {
                    tableWidth = childWidget.getTableCellWidth();
                }
                if (!(childWidget.wrapTextAround) && childWidget.y <= point.y && (childWidget.y + childWidget.height) >= point.y) {
                    return childWidget;
                }
                if ((childWidget.wrapTextAround &&
                    (childWidget.x <= point.x && (childWidget.x + tableWidth) >= point.x &&
                        childWidget.y <= point.y && (childWidget.y + childWidget.height) >= point.y))) {
                    return childWidget;
                }
            }
        }
        return undefined;
    };
    TableResizer.prototype.getTableCellWidget = function (cursorPoint) {
        var widget = undefined;
        var currentPage = this.owner.documentHelper.currentPage;
        if (!isNullOrUndefined(currentPage)) {
            for (var i = 0; i < currentPage.bodyWidgets.length; i++) {
                var bodyWidget = currentPage.bodyWidgets[parseInt(i.toString(), 10)];
                widget = bodyWidget.getTableCellWidget(cursorPoint);
                if (!isNullOrUndefined(widget)) {
                    break;
                }
                if (this.documentHelper.owner.layoutType === 'Pages' && this.viewer.owner.enableHeaderAndFooter) {
                    var body = currentPage.headerWidget;
                    widget = body.getTableCellWidget(cursorPoint);
                    if (!isNullOrUndefined(widget)) {
                        break;
                    }
                    var bodywid = currentPage.footerWidget;
                    widget = bodywid.getTableCellWidget(cursorPoint);
                    if (!isNullOrUndefined(widget)) {
                        break;
                    }
                }
            }
        }
        if (isNullOrUndefined(widget) && !isNullOrUndefined(currentPage.nextPage)) {
            for (var i = 0; i < currentPage.nextPage.bodyWidgets.length; i++) {
                widget = currentPage.nextPage.bodyWidgets[parseInt(i.toString(), 10)].getTableCellWidget(cursorPoint);
                if (!isNullOrUndefined(widget)) {
                    return widget;
                }
            }
        }
        if (isNullOrUndefined(widget) && !isNullOrUndefined(currentPage.previousPage)) {
            for (var i = 0; i < currentPage.previousPage.bodyWidgets.length; i++) {
                widget = currentPage.previousPage.bodyWidgets[parseInt(i.toString(), 10)].getTableCellWidget(cursorPoint);
                if (!isNullOrUndefined(widget)) {
                    return widget;
                }
            }
        }
        return widget;
    };
    TableResizer.prototype.updateRowHeight = function (row, dragValue) {
        var rowFormat = row.rowFormat;
        if (rowFormat.heightType === 'Auto') {
            rowFormat.heightType = 'AtLeast';
            var row_1 = rowFormat.ownerBase;
            var currentHeight = this.owner.documentHelper.layout.getRowHeight(row_1, [row_1]);
            //the minimum height of the Row in MS word is 2.7 points which is equal to 3.6 pixel.
            if (currentHeight + dragValue >= 2.7 && rowFormat.height !== currentHeight + dragValue) {
                rowFormat.height = currentHeight + dragValue;
            }
        }
        else {
            //the minimum height of the Row in MS word is 2.7 points which is equal to 3.6 pixel.
            var currentHeight = this.getRowFormatHeight(row);
            if (rowFormat.height !== currentHeight + dragValue) {
                rowFormat.height = currentHeight + dragValue < 2.7 ? 2.7 : currentHeight + dragValue;
            }
        }
    };
    TableResizer.prototype.getRowFormatHeight = function (row) {
        var height = 0;
        if (row.rowFormat.heightType === 'Exactly') {
            height = row.rowFormat.height;
        }
        else {
            var cellHeight = void 0;
            for (var i = 0; i < row.childWidgets.length; i++) {
                var cellWidget = row.childWidgets[i];
                if (cellWidget instanceof TableCellWidget) {
                    cellHeight = cellWidget.height;
                }
                if (height < cellHeight) {
                    height = cellHeight;
                }
            }
        }
        return height;
    };
    //Resize Table cell
    TableResizer.prototype.resizeTableCellColumn = function (dragValue) {
        var table = this.currentResizingTable;
        if (!this.owner.isTableMarkerDragging) {
            dragValue = table.isBidiTable && this.resizerPosition !== 0 ? -dragValue : dragValue;
        }
        if (isNullOrUndefined(table) || dragValue === 0 || isNullOrUndefined(table.childWidgets) || this.resizerPosition < 0) {
            return;
        }
        var selectionFlag = true;
        var selection = this.owner.selectionModule;
        this.owner.editorModule.setOffsetValue(selection);
        table = table.combineWidget(this.viewer);
        if (isNullOrUndefined(this.currentResizingTable.tableHolder)) {
            //When combining the table. If the table is destroyed. Need to update the currentResizing property.
            this.currentResizingTable = table;
        }
        this.owner.isLayoutEnabled = false;
        // table.PreserveGrid = true;
        this.setPreferredWidth(table);
        var containerWidth = table.getOwnerWidth(true);
        var newIndent = table.leftIndent;
        var tableAlignment = table.tableFormat.tableAlignment;
        if (!selection.isEmpty) {
            selectionFlag = this.resizeColumnWithSelection(selection, table, dragValue);
        }
        if (!selectionFlag) {
            this.updateGridValue(table, true);
            return;
        }
        var cellwidget = this.getTableCellWidget(this.startingPoint);
        var spannedCell = this.isCellSpannedFromPreviousRows(cellwidget);
        if (this.resizerPosition === 0 || (!isNullOrUndefined(cellwidget) && isNullOrUndefined(cellwidget.previousWidget)
            && (table.isBidiTable ? cellwidget.x <= this.startingPoint.x : cellwidget.x >= this.startingPoint.x)) &&
            (!spannedCell || spannedCell.length === 0)) {
            // Todo: need to handle the resizing of first column and table indent.
            var columnIndex = this.resizerPosition;
            var rightColumn = table.tableHolder.columns[parseInt(columnIndex.toString(), 10)];
            var width = rightColumn.preferredWidth;
            var rightColumnCollection = this.getColumnCells(table, columnIndex, false);
            var leastGridBefore = 0;
            var newGridBefore = 0;
            for (var i = 0; i < rightColumnCollection.length; i++) {
                var cell = rightColumnCollection[parseInt(i.toString(), 10)];
                var currentRow = cell.ownerRow;
                if (i === 0) {
                    leastGridBefore = this.getLeastGridBefore(table, currentRow);
                }
                var rowFormat = currentRow.rowFormat;
                var minWidth = HelperMethods.round(cell.getMinimumPreferredWidth(), 2);
                if (table.isBidiTable) {
                    dragValue = dragValue > 0 ? dragValue : cell.cellFormat.preferredWidth + dragValue <= minWidth ?
                        -HelperMethods.round(cell.cellFormat.preferredWidth - minWidth, 2) : dragValue;
                }
                else {
                    if (cell.cellFormat.preferredWidth - dragValue <= minWidth) {
                        dragValue = HelperMethods.round(cell.cellFormat.preferredWidth - minWidth, 2);
                    }
                }
                if (rowFormat.beforeWidth > 0) {
                    newGridBefore = rowFormat.beforeWidth + dragValue;
                    if (newGridBefore >= 0) {
                        // If the selected row contains beforeWidth value
                        this.updateGridBefore(currentRow, dragValue);
                        this.increaseOrDecreaseWidth(cell, dragValue, false);
                    }
                    else {
                        if (newGridBefore < leastGridBefore) {
                            newIndent = table.leftIndent + newGridBefore;
                            this.updateGridBefore(currentRow, -rowFormat.beforeWidth);
                            this.increaseOrDecreaseWidth(cell, dragValue, false);
                        }
                    }
                }
                else {
                    var currentIndent = table.tableFormat.leftIndent;
                    if (leastGridBefore === 0) {
                        if (table.isBidiTable && !this.owner.isTableMarkerDragging) {
                            newIndent = currentIndent - dragValue;
                            this.increaseOrDecreaseWidth(cell, -dragValue, false);
                        }
                        else {
                            // Normal table cell resizing scenario
                            newIndent = currentIndent + dragValue;
                            this.increaseOrDecreaseWidth(cell, dragValue, false);
                        }
                    }
                    else {
                        var difference = leastGridBefore - dragValue;
                        if (difference >= 0) {
                            // Resized table document table cell resizing beyond the table width scenario
                            newIndent = currentIndent + dragValue;
                            this.increaseOrDecreaseWidth(cell, dragValue, false);
                        }
                        else {
                            // Resized table document table cell value decreasing to the table width scenario
                            newIndent = currentIndent + leastGridBefore;
                            this.increaseOrDecreaseWidth(cell, dragValue, false);
                            this.updateGridBefore(currentRow, dragValue - leastGridBefore);
                        }
                    }
                }
            }
            if (newGridBefore < 0 || leastGridBefore !== 0) {
                for (var j = 0; j < table.childWidgets.length; j++) {
                    var tableRow = table.childWidgets[parseInt(j.toString(), 10)];
                    if (rightColumnCollection.indexOf(tableRow.childWidgets[0]) === -1) {
                        if (newGridBefore > 0 && leastGridBefore > 0) {
                            continue;
                        }
                        else if (newGridBefore < 0) {
                            this.updateGridBefore(tableRow, -newGridBefore);
                        }
                        else if (leastGridBefore - dragValue >= 0) {
                            this.updateGridBefore(tableRow, -dragValue);
                        }
                        else if (leastGridBefore - dragValue < 0) {
                            this.updateGridBefore(tableRow, -leastGridBefore);
                        }
                    }
                }
            }
            var dragOffset = dragValue;
            if (tableAlignment !== 'Left' && (table.tableHolder.getTotalWidth(0) > containerWidth) && table.tableFormat.preferredWidthType === 'Auto') {
                if (table.tableHolder.isFitColumns(containerWidth, table.tableHolder.tableWidth, table.tableFormat.preferredWidthType === 'Auto')) {
                    table.tableHolder.fitColumns(containerWidth, table.tableHolder.tableWidth, table.tableFormat.preferredWidthType === 'Auto', table.tableFormat.allowAutoFit);
                }
                else {
                    rightColumn.preferredWidth = width;
                }
                dragOffset = 0;
            }
            if (tableAlignment === 'Center'
                && (table.tableHolder.getTotalWidth(0) < containerWidth || table.tableFormat.preferredWidthType !== 'Auto')) {
                dragOffset = dragOffset / 2;
            }
            table.tableFormat.leftIndent = tableAlignment === 'Left' ? newIndent : 0;
            table.updateWidth(dragValue, this.isCurrentTableResizing(table));
            this.updateGridValue(table, true, dragOffset);
        }
        else if (table !== null && this.resizerPosition === table.tableHolder.columns.length || (!isNullOrUndefined(cellwidget)
            && isNullOrUndefined(cellwidget.nextWidget) && cellwidget.x + cellwidget.margin.left <= this.startingPoint.x
            && cellwidget.columnIndex !== this.resizerPosition) && (!spannedCell || spannedCell.length === 0)) {
            // Todo: need to handle the resizing of last column and table width.
            this.resizeColumnAtLastColumnIndex(table, dragValue);
        }
        else {
            if (this.resizerPosition === -1) {
                this.owner.isLayoutEnabled = true;
                return;
            }
            this.resizeCellAtMiddle(table, dragValue);
        }
        // table.PreserveGrid = false;
        this.owner.isLayoutEnabled = true;
        selection.selectPosition(selection.start, selection.end);
        if (this.owner.documentHelper.isRowOrCellResizing) {
            this.owner.editor.startParagraph = undefined;
        }
    };
    TableResizer.prototype.isCurrentTableResizing = function (table) {
        var isCurrentTableResizing = false;
        if (!isNullOrUndefined(this.owner) && (this.owner.isTableMarkerDragging || this.documentHelper.isRowOrCellResizing)) {
            isCurrentTableResizing = this.currentResizingTable === table;
        }
        return isCurrentTableResizing;
    };
    TableResizer.prototype.resizeColumnWithSelection = function (selection, table, dragValue) {
        //const newIndent: number = table.leftIndent;
        var cellwidget = this.getTableCellWidget(this.startingPoint);
        if (cellwidget && (selection.selectedWidgets.containsKey(cellwidget) || (cellwidget.previousWidget
            && selection.selectedWidgets.containsKey((cellwidget.previousWidget))))
            || this.owner.isTableMarkerDragging) {
            var selectedCells = selection.getSelectedCells();
            if (this.resizerPosition === 0) {
                if (table.isBidiTable) {
                    dragValue = dragValue > 0 ? dragValue :
                        cellwidget.cellFormat.preferredWidth + dragValue <= HelperMethods.round(cellwidget.getMinimumPreferredWidth(), 2) ?
                            -HelperMethods.round(cellwidget.cellFormat.preferredWidth - cellwidget.getMinimumPreferredWidth(), 2) :
                            dragValue;
                }
                this.resizeColumnAtStart(table, dragValue, selectedCells);
            }
            else if (table !== null && (this.resizerPosition === table.tableHolder.columns.length || (!isNullOrUndefined(cellwidget)
                && isNullOrUndefined(cellwidget.nextWidget) && cellwidget.x + cellwidget.margin.left <= this.startingPoint.x
                && cellwidget.columnIndex !== this.resizerPosition))) {
                var leftColumnCollection = this.owner.isTableMarkerDragging ?
                    this.getLastCells(table, selectedCells) : this.getColumnCells(table, this.resizerPosition, true);
                for (var i = 0; i < leftColumnCollection.length; i++) {
                    var cell = leftColumnCollection[parseInt(i.toString(), 10)];
                    if (selectedCells.indexOf(cell) !== -1) {
                        if (dragValue < 0 && !(cell.cellFormat.preferredWidth + dragValue > cell.ownerColumn.minimumWidth)) {
                            dragValue = -(cell.cellFormat.preferredWidth - cell.ownerColumn.minimumWidth);
                        }
                        this.increaseOrDecreaseWidth(cell, dragValue, true);
                    }
                }
                //Updates the grid after value for all the rows.
                this.updateRowsGridAfterWidth(table);
                table.updateWidth(dragValue);
                table.tableFormat.allowAutoFit = false;
                this.updateGridValue(table, true, dragValue);
            }
            else {
                if (this.resizerPosition === -1) {
                    return false;
                }
                var columnIndex = this.resizerPosition;
                var leftColumnCollection = this.owner.isTableMarkerDragging ? this.getMiddleCells(table) :
                    this.getColumnCells(table, columnIndex, true);
                var rightColumnCollection = this.owner.isTableMarkerDragging ? []
                    : this.getColumnCells(table, columnIndex, false);
                if (leftColumnCollection.length > 0) {
                    for (var i = 0; i < leftColumnCollection.length; i++) {
                        if (selectedCells.indexOf(leftColumnCollection[parseInt(i.toString(), 10)]) === -1) {
                            leftColumnCollection.splice(i, 1);
                            i--;
                        }
                    }
                }
                if (rightColumnCollection.length > 0) {
                    for (var i = 0; i < rightColumnCollection.length; i++) {
                        if (selectedCells.indexOf(rightColumnCollection[parseInt(i.toString(), 10)]) === -1) {
                            rightColumnCollection.splice(i, 1);
                            i--;
                        }
                    }
                }
                //Getting the adjacent cell collections for left side selected cells in the right column collection.
                if (!this.owner.isTableMarkerDragging) {
                    if (leftColumnCollection.length === 0 && rightColumnCollection.length > 0) {
                        for (var i = 0; i < rightColumnCollection.length; i++) {
                            var cell = rightColumnCollection[parseInt(i.toString(), 10)];
                            if (cell.previousWidget) {
                                leftColumnCollection.push(cell.previousWidget);
                            }
                        }
                    }
                    else if (rightColumnCollection.length === 0 && leftColumnCollection.length > 0) {
                        for (var i = 0; i < leftColumnCollection.length; i++) {
                            var cell = leftColumnCollection[parseInt(i.toString(), 10)];
                            if (cell.nextWidget) {
                                rightColumnCollection.push(cell.nextWidget);
                            }
                        }
                    }
                }
                dragValue = this.changeWidthOfCells(table, leftColumnCollection, rightColumnCollection, dragValue);
                if (table.tableFormat.allowAutoFit) {
                    table.updateWidth(dragValue);
                }
                table.tableFormat.allowAutoFit = false;
                this.updateGridValue(table, true, dragValue);
            }
            selection.selectPosition(selection.start, selection.end);
        }
        return false;
    };
    TableResizer.prototype.resizeColumnAtStart = function (table, dragValue, selectedCells) {
        var newIndent = table.leftIndent;
        //const rightColumnCollection: TableCellWidget[] = this.getColumnCells(table, this.resizerPosition, false);
        var offset = 0;
        var selectedRow = selectedCells[0].ownerRow;
        var rowFormat = selectedRow.rowFormat;
        if (rowFormat.beforeWidth > 0) {
            var newGridBefore = rowFormat.beforeWidth + dragValue;
            if (newGridBefore > 0) {
                for (var i = 0; i < table.childWidgets.length; i++) {
                    var tableRow = table.childWidgets[parseInt(i.toString(), 10)];
                    if (selectedCells.indexOf(tableRow.childWidgets[0]) !== -1) {
                        var cellWidget = tableRow.childWidgets[0];
                        if (!isNullOrUndefined(cellWidget) &&
                            cellWidget.cellFormat.preferredWidth - dragValue <= cellWidget.ownerColumn.minimumWidth) {
                            dragValue = cellWidget.cellFormat.preferredWidth - cellWidget.ownerColumn.minimumWidth;
                        }
                        this.updateGridBefore(tableRow, dragValue);
                        this.increaseOrDecreaseWidth(cellWidget, dragValue, false);
                    }
                }
            }
            else {
                var leastGridBefore = this.getLeastGridBefore(table, selectedRow);
                if (newGridBefore < leastGridBefore && offset !== newGridBefore) {
                    newIndent = table.leftIndent + newGridBefore;
                    table.tableFormat.leftIndent = newIndent >= -1440 ? (newIndent <= 1440 ? newIndent : 1440) : -1440;
                    for (var i = 0; i < table.childWidgets.length; i++) {
                        var tableRow = table.childWidgets[parseInt(i.toString(), 10)];
                        if (selectedCells.indexOf(tableRow.childWidgets[0]) !== -1) {
                            this.updateGridBefore(tableRow, -tableRow.rowFormat.beforeWidth);
                            this.increaseOrDecreaseWidth(tableRow.childWidgets[0], dragValue, false);
                        }
                        else {
                            this.updateGridBefore(tableRow, -newGridBefore);
                        }
                    }
                }
            }
        }
        else {
            if (dragValue < 0) {
                if (table.isBidiTable) {
                    this.updateWidthForCells(table, selectedCells, -dragValue);
                }
                else {
                    newIndent = table.leftIndent + dragValue;
                    table.tableFormat.leftIndent = newIndent >= -1440 ? (newIndent <= 1440 ? newIndent : 1440) : -1440;
                    this.updateWidthForCells(table, selectedCells, dragValue);
                }
            }
            else {
                var leastGridBefore = this.getLeastGridBefore(table, selectedRow);
                var currentTableIndent = table.tableFormat.leftIndent;
                if (currentTableIndent === 0) {
                    for (var i = 0; i < table.childWidgets.length; i++) {
                        var tableRow = table.childWidgets[parseInt(i.toString(), 10)];
                        if (selectedCells.indexOf(tableRow.childWidgets[0]) !== -1) {
                            /* eslint-disable-next-line max-len */
                            var nextCellWidget = tableRow.childWidgets[0].nextWidget;
                            if (!isNullOrUndefined(nextCellWidget) &&
                                nextCellWidget.cellFormat.preferredWidth - nextCellWidget.ownerColumn.minimumWidth <= dragValue) {
                                var isContinue = true;
                                var newRightWidth = void 0;
                                while (isContinue) {
                                    newRightWidth = HelperMethods.round(nextCellWidget.cellFormat.preferredWidth - dragValue, 2);
                                    if (newRightWidth >= nextCellWidget.ownerColumn.minimumWidth) {
                                        isContinue = false;
                                    }
                                    else {
                                        dragValue += newRightWidth - nextCellWidget.ownerColumn.minimumWidth;
                                    }
                                }
                            }
                            if (table.isBidiTable) {
                                this.increaseOrDecreaseWidth(tableRow.childWidgets[0], -dragValue, false);
                            }
                            else {
                                this.updateGridBefore(tableRow, dragValue);
                                this.increaseOrDecreaseWidth(tableRow.childWidgets[0], dragValue, false);
                            }
                        }
                    }
                }
                else {
                    var difference = leastGridBefore - dragValue;
                    if (difference > 0) {
                        newIndent = table.leftIndent + dragValue;
                        table.tableFormat.leftIndent = newIndent >= -1440 ? (newIndent <= 1440 ? newIndent : 1440) : -1440;
                        this.updateWidthForCells(table, selectedCells, dragValue);
                    }
                    else {
                        newIndent = table.leftIndent + leastGridBefore;
                        table.tableFormat.leftIndent = newIndent >= -1440 ? (newIndent <= 1440 ? newIndent : 1440) : -1440;
                        for (var i = 0; i < table.childWidgets.length; i++) {
                            var tableRow = table.childWidgets[parseInt(i.toString(), 10)];
                            if (selectedCells.indexOf(tableRow.childWidgets[0]) !== -1) {
                                this.increaseOrDecreaseWidth(tableRow.childWidgets[0], dragValue, false);
                                this.updateGridBefore(tableRow, dragValue - leastGridBefore);
                            }
                            else {
                                this.updateGridBefore(tableRow, -leastGridBefore);
                            }
                        }
                    }
                }
            }
        }
        table.tableFormat.allowAutoFit = false;
        this.updateGridValue(table, true, dragValue);
    };
    TableResizer.prototype.updateWidthForCells = function (table, selectedCells, dragValue) {
        for (var i = 0; i < table.childWidgets.length; i++) {
            var tableRow = table.childWidgets[parseInt(i.toString(), 10)];
            if (selectedCells.indexOf(tableRow.childWidgets[0]) !== -1) {
                this.increaseOrDecreaseWidth(tableRow.childWidgets[0], dragValue, false);
            }
            else {
                this.updateGridBefore(tableRow, -dragValue);
            }
        }
    };
    TableResizer.prototype.resizeColumnAtLastColumnIndex = function (table, dragValue) {
        var tableAlignment = table.tableFormat.tableAlignment;
        var leftColumnCollection = this.owner.isTableMarkerDragging ? this.getLastCells(table) :
            this.getColumnCells(table, this.resizerPosition, true);
        for (var i = 0; i < leftColumnCollection.length; i++) {
            var cell = leftColumnCollection[parseInt(i.toString(), 10)];
            if (!isNullOrUndefined(cell)) {
                if (dragValue < 0 && !(cell.cellFormat.preferredWidth + dragValue > cell.ownerColumn.minimumWidth)) {
                    dragValue = -(cell.cellFormat.preferredWidth - cell.ownerColumn.minimumWidth);
                }
                this.increaseOrDecreaseWidth(cell, dragValue, true);
            }
        }
        //Updates the grid after value for all the rows.
        this.updateRowsGridAfterWidth(table);
        var isCurrentTableResizing = this.isCurrentTableResizing(table);
        if (table.tableFormat.preferredWidth || isCurrentTableResizing) {
            table.tableFormat.allowAutoFit = false;
            table.updateWidth(dragValue, isCurrentTableResizing, true);
        }
        var dragOffset = dragValue;
        if (tableAlignment === 'Right') {
            dragOffset = 0;
        }
        else if (tableAlignment === 'Center') {
            dragOffset = dragOffset / 2;
        }
        this.updateGridValue(table, true, dragOffset);
    };
    TableResizer.prototype.resizeCellAtMiddle = function (table, dragValue) {
        var leftColumnCollection = this.owner.isTableMarkerDragging ? this.getMiddleCells(table) :
            this.getColumnCells(table, this.resizerPosition, true);
        var rightColumnCollection = this.owner.isTableMarkerDragging ? []
            : this.getColumnCells(table, this.resizerPosition, false);
        //Getting the adjacent cell collections for left side selected cells in the right column collection.
        if (!this.owner.isTableMarkerDragging) {
            if (leftColumnCollection.length === 0 && rightColumnCollection.length > 0) {
                for (var i = 0; i < rightColumnCollection.length; i++) {
                    var cell = rightColumnCollection[parseInt(i.toString(), 10)];
                    if (cell.previousWidget) {
                        leftColumnCollection.push(cell.previousWidget);
                    }
                }
            }
            else if (rightColumnCollection.length === 0 && leftColumnCollection.length > 0) {
                for (var i = 0; i < leftColumnCollection.length; i++) {
                    var cell = leftColumnCollection[parseInt(i.toString(), 10)];
                    if (cell.nextWidget) {
                        rightColumnCollection.push(cell.nextWidget);
                    }
                }
            }
        }
        //Adjust the dragvalue based on the minimum width of the cells.
        if (dragValue >= 0) {
            for (var i = 0; i < leftColumnCollection.length; i++) {
                var cell = leftColumnCollection[parseInt(i.toString(), 10)];
                var nextCellWidget = cell.nextWidget;
                if (!isNullOrUndefined(nextCellWidget)
                    && nextCellWidget.cellFormat.preferredWidth - nextCellWidget.ownerColumn.minimumWidth <= dragValue) {
                    var isContinue = true;
                    var newRightWidth = void 0;
                    while (isContinue) {
                        newRightWidth = HelperMethods.round(nextCellWidget.cellFormat.preferredWidth - dragValue, 2);
                        if (newRightWidth >= nextCellWidget.ownerColumn.minimumWidth) {
                            isContinue = false;
                        }
                        else {
                            dragValue += newRightWidth - nextCellWidget.ownerColumn.minimumWidth;
                        }
                    }
                }
            }
        }
        else {
            for (var i = 0; i < rightColumnCollection.length; i++) {
                var cell = rightColumnCollection[parseInt(i.toString(), 10)];
                var previousCellWidget = cell.previousWidget;
                if (!isNullOrUndefined(previousCellWidget)
                    && previousCellWidget.cellFormat.preferredWidth - previousCellWidget.ownerColumn.minimumWidth >= dragValue) {
                    var isContinue = true;
                    var newRightWidth = void 0;
                    while (isContinue) {
                        newRightWidth = HelperMethods.round(previousCellWidget.cellFormat.preferredWidth + dragValue, 2);
                        if (newRightWidth >= previousCellWidget.ownerColumn.minimumWidth) {
                            isContinue = false;
                        }
                        else {
                            dragValue -= newRightWidth - previousCellWidget.ownerColumn.minimumWidth;
                        }
                    }
                }
            }
        }
        this.changeWidthOfCells(table, leftColumnCollection, rightColumnCollection, dragValue);
        if (table.tableFormat.allowAutoFit) {
            table.updateWidth(dragValue);
        }
        table.tableFormat.allowAutoFit = false;
        this.updateGridValue(table, true, dragValue);
    };
    TableResizer.prototype.getLastCells = function (table, selectedCells) {
        var cells = [];
        var cellWidget = this.documentHelper.selection.start.paragraph.associatedCell;
        if (!cellWidget || !cellWidget.ownerRow) {
            return cells;
        }
        var referenceRow = cellWidget.ownerRow;
        var referenceRowWidth = this.getRowWidth(referenceRow, true);
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            var cell = row.lastChild;
            if (!this.documentHelper.selection.isEmpty) {
                if (selectedCells.indexOf(cell) !== -1) {
                    cells.push(cell);
                }
            }
            else {
                if (this.getRowWidth(row, true) >= referenceRowWidth) {
                    if (cell) {
                        cells.push(cell);
                    }
                }
            }
        }
        return cells;
    };
    TableResizer.prototype.getMiddleCells = function (table) {
        var cells = [];
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            for (var j = 0; j < row.childWidgets.length; j++) {
                var cell = row.childWidgets[parseInt(j.toString(), 10)];
                if (cell.x <= this.startingPoint.x
                    && this.startingPoint.x <= cell.x + cell.margin.left + cell.margin.right + cell.width) {
                    cells.push(cell);
                }
            }
        }
        return cells;
    };
    TableResizer.prototype.updateGridValue = function (table, isUpdate, dragValue) {
        if (isUpdate) {
            table.calculateGrid();
            table.isGridUpdated = false;
        }
        table.buildTableColumns();
        table.isGridUpdated = true;
        this.viewer.owner.isLayoutEnabled = true;
        if (table.isInsideTable) {
            var parentTable = this.documentHelper.layout.getParentTable(table);
            this.documentHelper.layout.reLayoutTable(parentTable); // Need to optmize this.
        }
        else {
            this.documentHelper.layout.reLayoutTable(table);
        }
        this.owner.editorModule.getOffsetValue(this.documentHelper.selection);
        this.owner.editorModule.isSkipOperationsBuild = this.owner.enableCollaborativeEditing;
        this.owner.editorModule.reLayout(this.owner.selectionModule);
        this.owner.editorModule.isSkipOperationsBuild = false;
        if (dragValue) {
            if (table.isBidiTable && this.resizerPosition !== 0) {
                this.startingPoint.x -= HelperMethods.convertPointToPixel(dragValue);
            }
            else {
                this.startingPoint.x += HelperMethods.convertPointToPixel(dragValue);
            }
            // if (!this.documentHelper.selection.isEmpty) {
            this.resizerPosition = this.getCellReSizerPosition(this.startingPoint);
            // }
        }
    };
    TableResizer.prototype.getColumnCells = function (table, columnIndex, isLeftSideCollection) {
        var cells = [];
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            for (var j = 0; j < row.childWidgets.length; j++) {
                var cell = row.childWidgets[parseInt(j.toString(), 10)];
                if (isLeftSideCollection) {
                    if (cell.columnIndex + cell.cellFormat.columnSpan === columnIndex) {
                        cells.push(cell);
                    }
                }
                else {
                    if (cell.columnIndex === columnIndex) {
                        cells.push(cell);
                    }
                }
            }
        }
        return cells;
    };
    TableResizer.prototype.updateGridBefore = function (row, offset) {
        if (row.rowFormat.beforeWidth + offset !== row.rowFormat.beforeWidth) {
            row.rowFormat.beforeWidth = HelperMethods.round(row.rowFormat.beforeWidth + offset, 2);
            row.rowFormat.gridBeforeWidth = row.rowFormat.beforeWidth;
        }
    };
    TableResizer.prototype.getLeastGridBefore = function (table, ignoreRow) {
        var gridBefore = 0;
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            if (row !== ignoreRow) {
                if (row.rowFormat.beforeWidth !== 0 && (gridBefore === 0 || row.rowFormat.beforeWidth < gridBefore)) {
                    gridBefore = row.rowFormat.beforeWidth;
                }
            }
        }
        return gridBefore;
    };
    TableResizer.prototype.increaseOrDecreaseWidth = function (cell, dragValue, isIncrease, isRightCellResized) {
        var preferredWidth = cell.cellFormat.preferredWidth;
        if (cell.cellFormat.preferredWidthType === 'Auto') {
            preferredWidth = cell.cellFormat.cellWidth;
            cell.cellFormat.preferredWidthType = 'Point';
        }
        var minimumWidth = HelperMethods.round(cell.getMinimumPreferredWidth(), 2);
        if (cell.cellFormat.preferredWidthType === 'Percent') {
            minimumWidth = cell.convertPointToPercent(minimumWidth);
        }
        // Margins properties usedd for internal purpose.
        if (isIncrease) {
            if (dragValue > 0 || this.resizerPosition === this.currentResizingTable.tableHolder.columns.length) {
                var nextCellWidget = cell.nextWidget;
                if (!isNullOrUndefined(nextCellWidget) && !this.owner.selection.isEmpty &&
                    nextCellWidget.cellFormat.preferredWidth - nextCellWidget.ownerColumn.minimumWidth <= dragValue) {
                    var isContinue = true;
                    var newRightWidth = void 0;
                    while (isContinue) {
                        newRightWidth = HelperMethods.round(nextCellWidget.cellFormat.preferredWidth - dragValue, 2);
                        if (newRightWidth >= nextCellWidget.ownerColumn.minimumWidth) {
                            nextCellWidget.cellFormat.preferredWidth = newRightWidth;
                            cell.cellFormat.preferredWidth = HelperMethods.round(preferredWidth + dragValue, 2);
                            isRightCellResized = true;
                            isContinue = false;
                        }
                        else {
                            dragValue += newRightWidth - cell.nextWidget.ownerColumn.minimumWidth;
                        }
                    }
                }
                else {
                    cell.cellFormat.preferredWidth =
                        HelperMethods.round(preferredWidth + dragValue > minimumWidth ? preferredWidth + dragValue : minimumWidth, 2);
                }
            }
            else if (isNullOrUndefined(cell.nextWidget)) {
                cell.cellFormat.preferredWidth =
                    HelperMethods.round(preferredWidth + dragValue > minimumWidth ? preferredWidth + dragValue : minimumWidth, 2);
            }
            else if (this.owner.isTableMarkerDragging) {
                cell.cellFormat.preferredWidth =
                    HelperMethods.round(preferredWidth + dragValue > minimumWidth ? preferredWidth + dragValue : minimumWidth, 2);
            }
        }
        else {
            var previousCellWidget = cell.previousWidget;
            if (dragValue < 0 && !isNullOrUndefined(previousCellWidget) &&
                previousCellWidget.cellFormat.preferredWidth - previousCellWidget.ownerColumn.minimumWidth >= dragValue) {
                var isContinue = true;
                var newRightWidth = void 0;
                while (isContinue) {
                    newRightWidth = HelperMethods.round(previousCellWidget.cellFormat.preferredWidth + dragValue, 2);
                    if (newRightWidth >= previousCellWidget.ownerColumn.minimumWidth) {
                        previousCellWidget.cellFormat.preferredWidth = newRightWidth;
                        cell.cellFormat.preferredWidth = HelperMethods.round(preferredWidth - dragValue, 2);
                        isContinue = false;
                    }
                    else {
                        dragValue -= newRightWidth - previousCellWidget.ownerColumn.minimumWidth;
                    }
                }
            }
            else {
                cell.cellFormat.preferredWidth =
                    HelperMethods.round(preferredWidth - dragValue > minimumWidth ? preferredWidth - dragValue : minimumWidth, 2);
            }
        }
        return isRightCellResized;
    };
    /* eslint-disable-next-line max-len */
    TableResizer.prototype.changeWidthOfCells = function (table, leftColumnCollection, rightColumnCollection, dragValue) {
        var isRightCellWidthUpdated = false;
        if (leftColumnCollection.length > 0) {
            var flag = false;
            for (var i = 0; i < leftColumnCollection.length; i++) {
                var cell = leftColumnCollection[parseInt(i.toString(), 10)];
                var nextCellWidget = cell.nextWidget;
                if (!isNullOrUndefined(nextCellWidget) &&
                    nextCellWidget.cellFormat.preferredWidth - nextCellWidget.ownerColumn.minimumWidth <= dragValue) {
                    var isContinue = true;
                    var newRightWidth = void 0;
                    while (isContinue) {
                        newRightWidth = HelperMethods.round(nextCellWidget.cellFormat.preferredWidth - dragValue, 2);
                        if (newRightWidth >= nextCellWidget.ownerColumn.minimumWidth) {
                            isContinue = false;
                        }
                        else {
                            dragValue += newRightWidth - nextCellWidget.ownerColumn.minimumWidth;
                        }
                    }
                }
                isRightCellWidthUpdated = this.increaseOrDecreaseWidth(cell, dragValue, true, isRightCellWidthUpdated);
                if (cell.cellIndex === cell.ownerRow.childWidgets.length - 1 &&
                    (!this.isCellSpannedFromPreviousRows(cell) || this.isCellSpannedFromPreviousRows(cell).length === 0)) {
                    flag = true;
                }
            }
            if (flag) {
                this.updateRowsGridAfterWidth(table);
            }
        }
        if (rightColumnCollection.length > 0 && !isRightCellWidthUpdated) {
            var diff = 0;
            for (var i = 0; i < rightColumnCollection.length; i++) {
                var cell = rightColumnCollection[parseInt(i.toString(), 10)];
                if (cell.cellIndex === 0 && cell.ownerRow.rowFormat.beforeWidth > 0) {
                    if (cell.cellFormat.preferredWidth - dragValue <= cell.ownerColumn.minimumWidth) {
                        dragValue = cell.cellFormat.preferredWidth - cell.ownerColumn.minimumWidth;
                    }
                    var newGridBefore = cell.ownerRow.rowFormat.beforeWidth + dragValue;
                    if (newGridBefore >= 0) {
                        this.updateGridBefore(cell.ownerRow, dragValue);
                    }
                    else {
                        if (diff !== newGridBefore) {
                            diff = newGridBefore;
                        }
                        cell.ownerRow.rowFormat.gridBeforeWidth = 0;
                        cell.ownerRow.rowFormat.beforeWidth = 0;
                        cell.ownerRow.rowFormat.gridBeforeWidthType = 'Auto';
                    }
                }
                else {
                    var previousCellWidget = cell.previousWidget;
                    if (dragValue < 0 && !isNullOrUndefined(previousCellWidget) &&
                        previousCellWidget.cellFormat.preferredWidth - previousCellWidget.ownerColumn.minimumWidth >= dragValue) {
                        var isContinue = true;
                        var newRightWidth = void 0;
                        while (isContinue) {
                            newRightWidth = HelperMethods.round(previousCellWidget.cellFormat.preferredWidth + dragValue, 2);
                            if (newRightWidth >= previousCellWidget.ownerColumn.minimumWidth) {
                                isContinue = false;
                            }
                            else {
                                dragValue -= newRightWidth - previousCellWidget.ownerColumn.minimumWidth;
                            }
                        }
                    }
                }
                this.increaseOrDecreaseWidth(cell, dragValue, false);
            }
            if (diff !== 0) {
                var newIndent = table.leftIndent + diff;
                table.tableFormat.leftIndent = newIndent >= -1440 ? (newIndent <= 1440 ? newIndent : 1440) : -1440;
                for (var j = 0; j < table.childWidgets.length; j++) {
                    var row = table.childWidgets[parseInt(j.toString(), 10)];
                    if (rightColumnCollection.indexOf(row.childWidgets[0]) === -1) {
                        this.updateGridBefore(row, diff > 0 ? diff : -diff);
                    }
                }
            }
        }
        return dragValue;
    };
    TableResizer.prototype.isCellSpannedFromPreviousRows = function (cell) {
        var spanedCell = [];
        if (isNullOrUndefined(cell) || isNullOrUndefined(cell.ownerRow) || isNullOrUndefined(cell.ownerRow.ownerTable)) {
            return undefined;
        }
        var table = cell.ownerRow.ownerTable;
        var currentRowIndex = cell.ownerRow.rowIndex;
        for (var i = 1; i <= currentRowIndex; i++) {
            var prevRow = table.childWidgets[currentRowIndex - i];
            if (isNullOrUndefined(prevRow) || prevRow.childWidgets.length === 0) {
                return undefined;
            }
            for (var j = 0; j < prevRow.childWidgets.length; j++) {
                var prevCell = prevRow.childWidgets[parseInt(j.toString(), 10)];
                var rowSpan = prevCell.cellFormat.rowSpan;
                if (rowSpan > 1) {
                    if ((prevCell.rowIndex + rowSpan - 1) >= currentRowIndex) {
                        spanedCell.push(prevCell);
                    }
                }
            }
        }
        return spanedCell;
    };
    TableResizer.prototype.updateRowsGridAfterWidth = function (table) {
        var maxRowWidth = HelperMethods.round(this.getMaxRowWidth(table, true), 2);
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            var currentRowWidth = HelperMethods.round(this.getRowWidth(row, true), 2);
            if (maxRowWidth >= currentRowWidth && row.rowFormat.afterWidth !== maxRowWidth - currentRowWidth) {
                var value = maxRowWidth - currentRowWidth;
                row.rowFormat.gridAfterWidth = value;
                row.rowFormat.afterWidth = value;
            }
        }
    };
    TableResizer.prototype.getRowWidth = function (row, toUpdateGridAfter) {
        var rowWidth = 0;
        if (toUpdateGridAfter) {
            rowWidth = rowWidth + row.rowFormat.beforeWidth;
        }
        for (var i = 0; i < row.childWidgets.length; i++) {
            var cell = row.childWidgets[parseInt(i.toString(), 10)];
            rowWidth += cell.cellFormat.preferredWidth;
            if (cell.cellIndex === row.childWidgets.length - 1) {
                var spannedCell = this.isCellSpannedFromPreviousRows(cell);
                if (!isNullOrUndefined(spannedCell) && spannedCell.length !== 0) {
                    for (var _i = 0, spannedCell_1 = spannedCell; _i < spannedCell_1.length; _i++) {
                        var cell_1 = spannedCell_1[_i];
                        rowWidth += cell_1.cellFormat.preferredWidth;
                    }
                }
            }
        }
        return rowWidth;
    };
    TableResizer.prototype.getMaxRowWidth = function (table, toUpdateGridAfter) {
        var width = 0;
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            var rowWidth = 0;
            if (toUpdateGridAfter) {
                rowWidth = rowWidth + row.rowFormat.beforeWidth;
            }
            for (var i_1 = 0; i_1 < row.childWidgets.length; i_1++) {
                var cell = row.childWidgets[parseInt(i_1.toString(), 10)];
                rowWidth += cell.cellFormat.preferredWidth;
            }
            if (width < rowWidth) {
                width = rowWidth;
            }
        }
        return width;
    };
    TableResizer.prototype.applyProperties = function (table, tableHistoryInfo) {
        if (isNullOrUndefined(tableHistoryInfo)) {
            return;
        }
        // PreserveGrid = true;
        if (tableHistoryInfo.tableHolder) {
            table.tableHolder = tableHistoryInfo.tableHolder.clone();
        }
        if (tableHistoryInfo.tableFormat !== null) {
            table.tableFormat.leftIndent = tableHistoryInfo.tableFormat.leftIndent;
            table.tableFormat.preferredWidth = tableHistoryInfo.tableFormat.preferredWidth;
            table.tableFormat.preferredWidthType = tableHistoryInfo.tableFormat.preferredWidthType;
            table.tableFormat.allowAutoFit = tableHistoryInfo.tableFormat.allowAutoFit;
        }
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            var rowFormat = tableHistoryInfo.rows[parseInt(i.toString(), 10)];
            row.rowFormat.gridBefore = rowFormat.gridBefore;
            row.rowFormat.gridBeforeWidth = rowFormat.gridBeforeWidth;
            row.rowFormat.gridBeforeWidthType = rowFormat.gridBeforeWidthType;
            row.rowFormat.gridAfter = rowFormat.gridAfter;
            row.rowFormat.gridAfterWidth = rowFormat.gridAfterWidth;
            row.rowFormat.gridAfterWidthType = rowFormat.gridAfterWidthType;
            for (var j = 0; j < row.childWidgets.length; j++) {
                var cell = row.childWidgets[parseInt(j.toString(), 10)];
                var cellFormat = rowFormat.cells[parseInt(j.toString(), 10)];
                cell.columnIndex = cellFormat.columnIndex;
                cell.cellFormat.columnSpan = cellFormat.columnSpan;
                cell.cellFormat.preferredWidth = cellFormat.preferredWidth;
                cell.cellFormat.preferredWidthType = cellFormat.preferredWidthType;
            }
        }
        var containerWidth = table.getOwnerWidth(true);
        var tableWidth = table.getTableClientWidth(containerWidth);
        //Sets the width to cells
        table.setWidthToCells(tableWidth, table.tableFormat.preferredWidthType === 'Auto');
        // PreserveGrid = false;
    };
    TableResizer.prototype.getActualWidth = function (row) {
        var width = 0;
        if (row.childWidgets.length > 0) {
            for (var i = 0; i < row.childWidgets.length; i++) {
                width += row.childWidgets[parseInt(i.toString(), 10)].cellFormat.cellWidth;
            }
        }
        return width;
    };
    TableResizer.prototype.setPreferredWidth = function (table) {
        for (var i = 0; i < table.childWidgets.length; i++) {
            var rw = table.childWidgets[parseInt(i.toString(), 10)];
            var rowFormat = rw.rowFormat;
            if (rowFormat.gridBefore > 0) {
                rowFormat.gridBeforeWidth = rowFormat.beforeWidth;
                rowFormat.gridBeforeWidthType = 'Point';
            }
            for (var j = 0; j < rw.childWidgets.length; j++) {
                var cell = rw.childWidgets[parseInt(j.toString(), 10)];
                cell.cellFormat.preferredWidth = cell.cellFormat.cellWidth;
                cell.cellFormat.preferredWidthType = 'Point';
            }
            if (rowFormat.gridAfter > 0) {
                rowFormat.gridAfterWidth = rowFormat.afterWidth;
                rowFormat.gridAfterWidthType = 'Point';
            }
        }
    };
    TableResizer.prototype.updateCellPreferredWidths = function (table) {
        var tableWidth = table.tableHolder.tableWidth;
        for (var i = 0; i < table.childWidgets.length; i++) {
            var row = table.childWidgets[parseInt(i.toString(), 10)];
            if (row.rowFormat.gridBefore > 0) {
                var width = table.tableHolder.getCellWidth(0, row.rowFormat.gridBefore, tableWidth);
                this.updateGridBeforeWidth(width, row);
            }
            for (var j = 0; j < row.childWidgets.length; j++) {
                var cell = row.childWidgets[parseInt(j.toString(), 10)];
                cell.updateWidth(table.tableHolder.getCellWidth(cell.columnIndex, cell.cellFormat.columnSpan, tableWidth));
            }
            if (row.rowFormat.gridAfter > 0) {
                /* eslint-disable-next-line max-len */
                this.updateGridAfterWidth(table.tableHolder.getCellWidth(row.childWidgets.length, row.rowFormat.gridAfter, tableWidth), row);
            }
        }
    };
    TableResizer.prototype.updateGridBeforeWidth = function (width, row) {
        var rowFormat = row.rowFormat;
        if (width !== rowFormat.beforeWidth) {
            rowFormat.beforeWidth = width;
            if (rowFormat.gridBeforeWidthType === 'Auto') {
                rowFormat.gridBeforeWidthType = 'Point';
            }
            if (rowFormat.gridBeforeWidthType === 'Point') {
                rowFormat.gridBeforeWidth = rowFormat.beforeWidth;
            }
            else {
                // The value is calculated from the pixel values hence, its converted to percent using method.
                var ownerWidth = row.ownerTable.getTableClientWidth(row.ownerTable.getOwnerWidth(true));
                var value = row.ownerTable.convertPointToPercent(rowFormat.beforeWidth, ownerWidth);
                rowFormat.gridBeforeWidth = value;
            }
        }
    };
    TableResizer.prototype.updateGridAfterWidth = function (width, row) {
        var rowFormat = row.rowFormat;
        if (width !== rowFormat.afterWidth) {
            rowFormat.afterWidth = width;
        }
        if (rowFormat.gridAfterWidthType === 'Auto') {
            rowFormat.gridAfterWidthType = 'Point';
        }
        if (rowFormat.gridAfterWidthType === 'Point') {
            rowFormat.gridAfterWidth = rowFormat.afterWidth;
        }
        else {
            // The value is calculated from the pixel values hence, its converted to percent using method.
            var ownerWidth = row.ownerTable.getTableClientWidth(row.ownerTable.getOwnerWidth(true));
            var value = row.ownerTable.convertPointToPercent(rowFormat.afterWidth, ownerWidth);
            rowFormat.gridAfterWidth = value;
        }
    };
    /**
     * @private
     * @returns {void}
     */
    TableResizer.prototype.destroy = function () {
        this.documentHelper = undefined;
        this.owner = undefined;
    };
    return TableResizer;
}());
export { TableResizer };
