"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAdaptableComputedCSSVar = exports.useAdaptableComputedCSSVars = exports.WithAdaptableComputedCSSVars = exports.AdaptableComputedCSSVarsContext = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const CSSNumericVariableWatch_1 = require("../components/Select/CSSNumericVariableWatch");
const debounce_1 = tslib_1.__importDefault(require("../Utilities/utils/debounce"));
const react_1 = require("react");
const computedCSSVarNames = [
    '--ab-cmp-select-menu__max-width',
    '--ab-cmp-select-menu__min-width',
    '--ab-cmp-select-menu__max-height',
];
const defaultValues = Object.fromEntries(computedCSSVarNames.map((varName) => [varName, 0]));
exports.AdaptableComputedCSSVarsContext = React.createContext(defaultValues);
const WithAdaptableComputedCSSVars = ({ children }) => {
    const [computedCSSVars, doSetComputedCSSVars] = React.useState(defaultValues);
    const setComputedCSSVars = (0, react_1.useMemo)(() => {
        return (0, debounce_1.default)(doSetComputedCSSVars, 300, { leading: false, trailing: true });
    }, [doSetComputedCSSVars]);
    return (React.createElement(exports.AdaptableComputedCSSVarsContext.Provider, { value: computedCSSVars },
        children,
        computedCSSVarNames.map((varName) => (React.createElement(CSSNumericVariableWatch_1.CSSNumericVariableWatch, { key: varName, varName: varName, onChange: (value) => {
                setComputedCSSVars((prev) => ({ ...prev, [varName]: value }));
            } })))));
};
exports.WithAdaptableComputedCSSVars = WithAdaptableComputedCSSVars;
const useAdaptableComputedCSSVars = () => {
    const context = React.useContext(exports.AdaptableComputedCSSVarsContext);
    return context;
};
exports.useAdaptableComputedCSSVars = useAdaptableComputedCSSVars;
const useAdaptableComputedCSSVar = (varName) => {
    const context = React.useContext(exports.AdaptableComputedCSSVarsContext);
    return context?.[varName];
};
exports.useAdaptableComputedCSSVar = useAdaptableComputedCSSVar;
