"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get getMostBottomEndPosition () {
        return getMostBottomEndPosition;
    },
    get getMostTopStartPosition () {
        return getMostTopStartPosition;
    },
    get normalizeCoordsIfNeeded () {
        return normalizeCoordsIfNeeded;
    }
});
const _number = require("../../helpers/number");
function normalizeCoordsIfNeeded(hot) {
    return (coords)=>{
        if (!coords) {
            return null;
        }
        const { rowIndexMapper, columnIndexMapper } = hot;
        if (rowIndexMapper.isHidden(coords.row) || columnIndexMapper.isHidden(coords.col)) {
            return null;
        }
        const mostTopStartCoords = getMostTopStartPosition(hot);
        const mostBottomEndCoords = getMostBottomEndPosition(hot);
        if (mostTopStartCoords === null || mostBottomEndCoords === null) {
            return null;
        }
        coords.row = (0, _number.clamp)(coords.row, mostTopStartCoords.row, mostBottomEndCoords.row);
        coords.col = (0, _number.clamp)(coords.col, mostTopStartCoords.col, mostBottomEndCoords.col);
        return coords;
    };
}
function getMostTopStartPosition(hot) {
    const { rowIndexMapper, columnIndexMapper } = hot;
    const { navigableHeaders } = hot.getSettings();
    let topRow = navigableHeaders && hot.countColHeaders() > 0 ? -hot.countColHeaders() : 0;
    let startColumn = navigableHeaders && hot.countRowHeaders() > 0 ? -hot.countRowHeaders() : 0;
    if (topRow === 0) {
        topRow = rowIndexMapper.getVisualFromRenderableIndex(topRow);
    }
    if (startColumn === 0) {
        startColumn = columnIndexMapper.getVisualFromRenderableIndex(startColumn);
    }
    if (topRow === null || startColumn === null) {
        return null;
    }
    return hot._createCellCoords(topRow, startColumn);
}
function getMostBottomEndPosition(hot) {
    const { rowIndexMapper, columnIndexMapper } = hot;
    const { navigableHeaders } = hot.getSettings();
    let bottomRow = rowIndexMapper.getRenderableIndexesLength() - 1;
    let endColumn = columnIndexMapper.getRenderableIndexesLength() - 1;
    if (bottomRow < 0) {
        if (!navigableHeaders || hot.countColHeaders() === 0) {
            return null;
        }
        bottomRow = -1;
    }
    if (endColumn < 0) {
        if (!navigableHeaders || hot.countColHeaders() === 0) {
            return null;
        }
        endColumn = -1;
    }
    return hot._createCellCoords(rowIndexMapper.getVisualFromRenderableIndex(bottomRow) ?? bottomRow, columnIndexMapper.getVisualFromRenderableIndex(endColumn) ?? endColumn);
}
