/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-0d43332 | Cube Dev Team */
import { useRef } from "react";
import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";

//#region src/utils/react/RenderCache.tsx
/**
* RenderCache optimizes rendering of item lists by reusing
* previously rendered elements for unchanged items.
*/
function RenderCache({ items, renderKeys, getKey, children }) {
	const cacheRef = useRef(/* @__PURE__ */ 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;
	});
	const currentKeys = new Set(items.map(getKey));
	for (const key of cacheRef.current.keys()) if (!currentKeys.has(key)) cacheRef.current.delete(key);
	return /* @__PURE__ */ jsx(Fragment$1, { children: rendered });
}

//#endregion
export { RenderCache };
//# sourceMappingURL=RenderCache.js.map