"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDynamicRowCellHeight = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@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, "]");
};
var useDynamicRowCellHeight = function (getRowCellKey) {
    if (getRowCellKey === void 0) { getRowCellKey = defaultGetRowCellKey; }
    var _a = (0, react_1.useState)({}), rowCellHeightsMap = _a[0], setRowCellHeightsMap = _a[1];
    var rowCellHeightsMapCache = (0, react_1.useRef)({});
    var flushHeightUpdates = (0, react_1.useCallback)((0, mdm_sdk_1.debounce)(function () { return setRowCellHeightsMap(__assign({}, rowCellHeightsMapCache.current)); }), []);
    var clearHeightsCache = (0, react_1.useCallback)(function () {
        rowCellHeightsMapCache.current = {};
    }, []);
    var getDynamicRowCellHeight = (0, react_1.useCallback)(function (cell) {
        var key = getRowCellKey(cell);
        return rowCellHeightsMap[key];
    }, [getRowCellKey, rowCellHeightsMap]);
    var changeRowCellHeight = (0, react_1.useCallback)((0, ramda_1.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 };
};
exports.useDynamicRowCellHeight = useDynamicRowCellHeight;
