"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.getRowAtRelativePosition = exports.getColAtRelativePosition = exports.getCellAtRelativePosition = exports.computeTargetColByX = exports.computeTargetRowByY = exports.getTargetRowAtConsiderBottomFrozen = exports.getTargetColAtConsiderRightFrozen = exports.getTargetRowAt = exports.getTargetColAt = exports.getCellAt = exports.getColAt = exports.getRowAt = void 0;

const tableHelper_1 = require("../tableHelper");

function getRowAt(absoluteY, _this) {
    var _a;
    const frozen = (0, tableHelper_1._getTargetFrozenRowAt)(_this, absoluteY);
    return frozen || (null !== (_a = getTargetRowAt(absoluteY, _this)) && void 0 !== _a ? _a : {
        top: -1,
        row: -1,
        bottom: -1,
        height: -1
    });
}

function getColAt(absoluteX, _this) {
    var _a;
    const frozen = (0, tableHelper_1._getTargetFrozenColAt)(_this, absoluteX);
    return frozen || (null !== (_a = getTargetColAt(absoluteX, _this)) && void 0 !== _a ? _a : {
        left: -1,
        col: -1,
        right: -1,
        width: 1
    });
}

function getCellAt(absoluteX, absoluteY, _this) {
    const rowInfo = getRowAt(absoluteY, _this), {row: row, top: top, bottom: bottom, height: height} = rowInfo, colInfo = getColAt(absoluteX, _this), {col: col, left: left, right: right, width: width} = colInfo;
    return {
        row: row,
        col: col,
        rect: {
            left: left,
            right: right,
            top: top,
            bottom: bottom,
            width: width,
            height: height
        }
    };
}

function getTargetColAt(absoluteX, _this) {
    if (0 === absoluteX) return {
        left: 0,
        col: 0,
        right: 0,
        width: 0
    };
    const candCol = computeTargetColByX(absoluteX, _this), right = _this.getColsWidth(0, candCol);
    return absoluteX >= right ? ((startCol, startRight) => {
        let left = startRight - _this.getColWidth(startCol);
        const {colCount: colCount} = _this.internalProps;
        for (let col = startCol; col < colCount; col++) {
            const width = _this.getColWidth(col), right = left + width;
            if (Math.round(left) <= absoluteX && absoluteX < Math.round(right)) return {
                left: left,
                col: col,
                right: right,
                width: width
            };
            left = right;
        }
        return null;
    })(candCol, right) : ((startCol, startRight) => {
        let right = startRight;
        for (let col = startCol; col >= 0; col--) {
            const width = _this.getColWidth(col), left = right - width;
            if (Math.round(left) <= absoluteX && absoluteX < Math.round(right)) return {
                left: left,
                col: col,
                right: right,
                width: width
            };
            right = left;
        }
        return null;
    })(candCol, right);
}

function getTargetRowAt(absoluteY, _this) {
    var _a;
    if (0 === absoluteY) return {
        top: 0,
        row: 0,
        bottom: 0,
        height: 0
    };
    const floorOrRound = !0 === (null === (_a = _this.options.customConfig) || void 0 === _a ? void 0 : _a._disableColumnAndRowSizeRound) ? Math.floor : Math.round, candRow = computeTargetRowByY(absoluteY, _this), bottom = _this.getRowsHeight(0, candRow);
    return absoluteY >= bottom ? ((startRow, startBottom) => {
        let top = startBottom - _this.getRowHeight(startRow);
        const {rowCount: rowCount} = _this.internalProps;
        for (let row = startRow; row < rowCount; row++) {
            const height = _this.getRowHeight(row), bottom = top + height;
            if (floorOrRound(top) <= floorOrRound(absoluteY) && floorOrRound(absoluteY) < floorOrRound(bottom)) return {
                top: top,
                row: row,
                bottom: bottom,
                height: height
            };
            top = bottom;
        }
        return null;
    })(candRow, bottom) : ((startRow, startBottom) => {
        let bottom = startBottom;
        for (let row = startRow; row >= 0; row--) {
            const height = _this.getRowHeight(row), top = bottom - height;
            if (floorOrRound(top) <= floorOrRound(absoluteY) && floorOrRound(absoluteY) < floorOrRound(bottom)) return {
                top: top,
                row: row,
                bottom: bottom,
                height: height
            };
            bottom = top;
        }
        return null;
    })(candRow, bottom);
}

function getTargetColAtConsiderRightFrozen(absoluteX, isConsider, _this) {
    if (0 === absoluteX) return {
        left: 0,
        col: 0,
        right: 0,
        width: 0
    };
    if (absoluteX -= _this.tableX, isConsider && absoluteX > _this.tableNoFrameWidth - _this.getRightFrozenColsWidth() && absoluteX < _this.tableNoFrameWidth && absoluteX <= _this.getAllColsWidth()) for (let i = 0; i < _this.rightFrozenColCount; i++) if (absoluteX > _this.tableNoFrameWidth - _this.getColsWidth(_this.colCount - i - 1, _this.colCount - 1)) return {
        col: _this.colCount - i - 1,
        left: void 0,
        right: void 0,
        width: void 0
    };
    return getTargetColAt(absoluteX, _this);
}

function getTargetRowAtConsiderBottomFrozen(absoluteY, isConsider, _this) {
    if (0 === absoluteY) return {
        top: 0,
        row: 0,
        bottom: 0,
        height: 0
    };
    if (absoluteY -= _this.tableY, isConsider && absoluteY > _this.tableNoFrameHeight - _this.getBottomFrozenRowsHeight() && absoluteY < _this.tableNoFrameHeight) for (let i = 0; i < _this.rightFrozenColCount; i++) if (absoluteY > _this.tableNoFrameHeight - _this.getRowsHeight(_this.rowCount - i - 1, _this.rowCount - 1)) return {
        row: _this.rowCount - i - 1,
        top: void 0,
        bottom: void 0,
        height: void 0
    };
    return getTargetRowAt(absoluteY, _this);
}

function computeTargetRowByY(absoluteY, _this) {
    let defaultRowHeight = _this.defaultRowHeight;
    return _this._rowRangeHeightsMap.get("$0$" + (_this.rowCount - 1)) && (defaultRowHeight = _this._rowRangeHeightsMap.get("$0$" + (_this.rowCount - 1)) / _this.rowCount), 
    Math.min(Math.ceil(absoluteY / defaultRowHeight), _this.rowCount - 1);
}

function computeTargetColByX(absoluteX, _this) {
    if (_this._colRangeWidthsMap.get("$0$" + (_this.colCount - 1))) {
        let startCol = 0, endCol = _this.colCount - 1;
        for (;endCol - startCol > 1; ) {
            const midCol = Math.floor((startCol + endCol) / 2);
            if (absoluteX < _this._colRangeWidthsMap.get(`$0$${midCol}`)) endCol = midCol; else {
                if (!(absoluteX > _this._colRangeWidthsMap.get(`$0$${midCol}`))) return midCol;
                startCol = midCol;
            }
        }
        return endCol;
    }
    return Math.min(Math.ceil(absoluteX / _this.internalProps.defaultColWidth), _this.colCount - 1);
}

function getCellAtRelativePosition(x, y, _this) {
    x -= _this.tableX;
    let topFrozen = !1;
    (y -= _this.tableY) > 0 && y < _this.getFrozenRowsHeight() && (topFrozen = !0);
    let leftFrozen = !1;
    x > 0 && x < _this.getFrozenColsWidth() && (leftFrozen = !0);
    let bottomFrozen = !1;
    y > _this.tableNoFrameHeight - _this.getBottomFrozenRowsHeight() && y < _this.tableNoFrameHeight && y <= _this.getAllRowsHeight() && (bottomFrozen = !0);
    let rightFrozen = !1;
    x > _this.tableNoFrameWidth - _this.getRightFrozenColsWidth() && x < _this.tableNoFrameWidth && x <= _this.getAllColsWidth() && (rightFrozen = !0);
    const colInfo = getTargetColAtConsiderRightFrozen((leftFrozen || rightFrozen ? x : x + _this.scrollLeft) + _this.tableX, rightFrozen, _this), rowInfo = getTargetRowAtConsiderBottomFrozen((topFrozen || bottomFrozen ? y : y + _this.scrollTop) + _this.tableY, bottomFrozen, _this);
    if (colInfo && rowInfo) {
        const {row: row, top: top, bottom: bottom, height: height} = rowInfo, {col: col, left: left, right: right, width: width} = colInfo;
        return {
            row: row,
            col: col,
            rect: {
                left: left,
                right: right,
                top: top,
                bottom: bottom,
                width: width,
                height: height
            }
        };
    }
    return {
        col: -1,
        row: -1
    };
}

function getColAtRelativePosition(x, _this) {
    let leftFrozen = !1;
    (x -= _this.tableX) > 0 && x < _this.getFrozenColsWidth() && (leftFrozen = !0);
    let rightFrozen = !1;
    x > _this.tableNoFrameWidth - _this.getRightFrozenColsWidth() && x < _this.tableNoFrameWidth && x <= _this.getAllColsWidth() && (rightFrozen = !0);
    const colInfo = getTargetColAtConsiderRightFrozen((leftFrozen || rightFrozen ? x : x + _this.scrollLeft) + _this.tableX, rightFrozen, _this);
    if (colInfo) {
        const {col: col} = colInfo;
        return col;
    }
    return -1;
}

function getRowAtRelativePosition(y, _this) {
    let topFrozen = !1;
    (y -= _this.tableY) > 0 && y < _this.getFrozenRowsHeight() && (topFrozen = !0);
    let bottomFrozen = !1;
    y > _this.tableNoFrameHeight - _this.getBottomFrozenRowsHeight() && y < _this.tableNoFrameHeight && y <= _this.getAllRowsHeight() && (bottomFrozen = !0);
    const rowInfo = getTargetRowAtConsiderBottomFrozen((topFrozen || bottomFrozen ? y : y + _this.scrollTop) + _this.tableY, bottomFrozen, _this);
    if (rowInfo) {
        const {row: row} = rowInfo;
        return row;
    }
    return -1;
}

exports.getRowAt = getRowAt, exports.getColAt = getColAt, exports.getCellAt = getCellAt, 
exports.getTargetColAt = getTargetColAt, exports.getTargetRowAt = getTargetRowAt, 
exports.getTargetColAtConsiderRightFrozen = getTargetColAtConsiderRightFrozen, exports.getTargetRowAtConsiderBottomFrozen = getTargetRowAtConsiderBottomFrozen, 
exports.computeTargetRowByY = computeTargetRowByY, exports.computeTargetColByX = computeTargetColByX, 
exports.getCellAtRelativePosition = getCellAtRelativePosition, exports.getColAtRelativePosition = getColAtRelativePosition, 
exports.getRowAtRelativePosition = getRowAtRelativePosition;
//# sourceMappingURL=get-cell-position.js.map
