"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAdaptableState = useAdaptableState;
const react_1 = require("react");
const AdaptableContext_1 = require("../../View/AdaptableContext");
function useAdaptableState(selector) {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const store = adaptable.adaptableStore;
    const [_refreshKey, setRefreshKey] = (0, react_1.useState)(0);
    const resultRef = (0, react_1.useRef)(null);
    const getResult = () => {
        if (!store) {
            return null;
        }
        const state = store.getCurrentStorageState();
        if (!state) {
            return null;
        }
        return selector ? selector(state) : state;
    };
    // don't refresh when the state changes
    // as we're under the adaptable context
    // and this means it will be refreshed automatically
    // useEffect(() => {
    //   if (!store) {
    //     return;
    //   }
    //   return store.onAny(() => {
    //     const newResult = getResult();
    //     if (newResult !== resultRef.current) {
    //       //only refresh when the result has changed
    //       setRefreshKey((prev) => prev + 1);
    //     }
    //   });
    // }, [store]);
    if (!adaptable || !adaptable.isReady) {
        return null;
    }
    const res = getResult();
    resultRef.current = res;
    return res;
}
