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 React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
var SyncedValueHeightsContext = createContext(null);
export var SyncedValueHeightsProvider = function (_a) {
    var children = _a.children;
    var _b = useState({}), heights = _b[0], setHeights = _b[1];
    var reportHeight = useCallback(function (uri, height) {
        if (!uri || !height || heights[uri] === height)
            return;
        setHeights(function (prev) {
            var _a;
            return (__assign(__assign({}, prev), (_a = {}, _a[uri] = height, _a)));
        });
    }, [heights]);
    var getHeight = useCallback(function (uri) { return (uri ? heights[uri] : 0); }, [heights]);
    var clear = useCallback(function () {
        setHeights({});
    }, []);
    var value = useMemo(function () { return ({ reportHeight: reportHeight, getHeight: getHeight, clear: clear }); }, [reportHeight, getHeight, clear]);
    return React.createElement(SyncedValueHeightsContext.Provider, { value: value }, children);
};
export var useSyncedValueHeights = function () {
    var context = useContext(SyncedValueHeightsContext);
    if (!context) {
        throw new Error('useSyncedValueHeights must be used within SyncedValueHeightsProvider');
    }
    return context;
};
