/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-c015414
 * Released under the MIT license.
 */

import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useRef } from 'react';
/**
 * RenderCache optimizes rendering of item lists by reusing
 * previously rendered elements for unchanged items.
 */
export function RenderCache({ items, renderKeys, getKey, children, }) {
    // Store previous renders
    const cacheRef = useRef(new Map());
    const rendered = items.map((item) => {
        const key = getKey(item);
        const shouldRerender = renderKeys.includes(key);
        const cached = cacheRef.current.get(key);
        if (!cached || shouldRerender) {
            const element = children(item);
            cacheRef.current.set(key, element);
            return element;
        }
        return cached;
    });
    // Optionally clean up cache for items no longer present
    const currentKeys = new Set(items.map(getKey));
    for (const key of cacheRef.current.keys()) {
        if (!currentKeys.has(key)) {
            cacheRef.current.delete(key);
        }
    }
    return _jsx(_Fragment, { children: rendered });
}

        