var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { useCallback, useRef, useState } from 'react';
import { curry } from 'ramda';
import { debounce } from '@reltio/mdm-sdk';
var defaultGetRowCellKey = function (_a) {
    var rowIndex = _a.rowIndex, columnData = _a.columnData, cellIndex = _a.cellIndex;
    return "".concat(columnData.id, "[").concat(rowIndex, "][").concat(cellIndex, "]");
};
export var useDynamicRowCellHeight = function (getRowCellKey) {
    if (getRowCellKey === void 0) { getRowCellKey = defaultGetRowCellKey; }
    var _a = useState({}), rowCellHeightsMap = _a[0], setRowCellHeightsMap = _a[1];
    var rowCellHeightsMapCache = useRef({});
    var flushHeightUpdates = useCallback(debounce(function () { return setRowCellHeightsMap(__assign({}, rowCellHeightsMapCache.current)); }), []);
    var clearHeightsCache = useCallback(function () {
        rowCellHeightsMapCache.current = {};
    }, []);
    var getDynamicRowCellHeight = useCallback(function (cell) {
        var key = getRowCellKey(cell);
        return rowCellHeightsMap[key];
    }, [getRowCellKey, rowCellHeightsMap]);
    var changeRowCellHeight = useCallback(curry(function (cell, height) {
        var key = getRowCellKey(cell);
        var currentHeight = rowCellHeightsMapCache.current[key];
        if (currentHeight !== height) {
            rowCellHeightsMapCache.current[key] = height;
            flushHeightUpdates();
        }
    }), [flushHeightUpdates, getRowCellKey]);
    return { getDynamicRowCellHeight: getDynamicRowCellHeight, changeRowCellHeight: changeRowCellHeight, clearHeightsCache: clearHeightsCache };
};
