import { useRef, useState } from 'react';
import { useAdaptable } from '../../View/AdaptableContext';
export function useAdaptableState(selector) {
    const adaptable = useAdaptable();
    const store = adaptable.adaptableStore;
    const [_refreshKey, setRefreshKey] = useState(0);
    const resultRef = 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;
}
