import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useEffect } from 'react';
import { useAppDispatch, useAppSelector } from 'src/app/redux/store';
import { getCachesFromEntries, sortCacheByTime } from 'src/lib/common/helpers/cache';
import { setCachesOnKey } from 'src/app/redux/slices/cache';
var allowedCacheCount = process.env.REACT_APP_ALLOWED_SEARCH_CACHE_COUNT;
export default function CacheWatcher(_a) {
    var cacheKey = _a.cacheKey;
    var dispatch = useAppDispatch();
    var cache = useAppSelector(function (state) { return state.cache; });
    useEffect(function () {
        var cachesOnKey = cache[cacheKey];
        if (cachesOnKey) {
            var cacheEntries = Object.entries(cachesOnKey);
            if (cacheEntries.length > allowedCacheCount) {
                var sortedEntries = sortCacheByTime(cacheEntries);
                var newEntries = sortedEntries.slice(1, allowedCacheCount + 1);
                var newCaches = getCachesFromEntries(newEntries);
                dispatch(setCachesOnKey({ key: cacheKey, caches: newCaches }));
            }
        }
    }, [cache, cacheKey]);
    return _jsx(_Fragment, {});
}
