/** @license MIT | @cube-dev/ui-kit v0.123.0 | Cube Dev Team */
import { TabPanelElement } from "./styled.js";
import { useEffect, useMemo, useRef } from "react";
import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
import { useTabPanel } from "react-aria";

//#region src/components/navigation/Tabs/TabPanel.tsx
/**
* Determines if a panel should be rendered based on prerender/keepMounted settings.
*/
function shouldRenderPanel(isActive, wasVisited, effectivePrerender, effectiveKeepMounted) {
	return !!effectivePrerender || isActive || !!effectiveKeepMounted && wasVisited;
}
/**
* Renders a single tab panel with proper ARIA attributes.
*/
function TabPanelRenderer({ tabKey, state, content, prerender, keepMounted, tabPrerender, tabKeepMounted, visitedKeys, panelStyles, qa, qaVal }) {
	const ref = useRef(null);
	const { tabPanelProps } = useTabPanel({ key: tabKey }, state, ref);
	const isActive = state.selectedKey === tabKey;
	const effectivePrerender = tabPrerender ?? prerender;
	const effectiveKeepMounted = tabKeepMounted ?? keepMounted;
	if (!shouldRenderPanel(isActive, visitedKeys.has(tabKey), effectivePrerender, effectiveKeepMounted)) return null;
	return /* @__PURE__ */ jsx(TabPanelElement, {
		...tabPanelProps,
		ref,
		mods: { active: isActive },
		qa: qa ?? "TabPanel",
		qaVal: qaVal ?? String(tabKey),
		styles: panelStyles,
		children: content
	});
}
/**
* Renders panels with content caching for `renderPanel`.
*
* Core principle: `renderPanel` is only called when the tab is active
* (or once on mount for `prerender`). Inactive panels use cached content.
*
* Caching behavior:
* - `keepMounted=true`: Cache content after first activation, reuse while inactive
* - `prerender=true`: Call `renderPanel` once on mount, reuse until active again
* - `panelCacheKeys`: Adds cache-key-based invalidation (lazy - only when active)
* - No caching props: Only active panel is rendered, unmount when inactive
*/
function CachedPanelRenderer({ parsedTabs, explicitPanels, state, renderPanel, panelCacheKeys, prerender, keepMounted, visitedKeys }) {
	const contentCacheRef = useRef(/* @__PURE__ */ new Map());
	/** Get the cache key for a panel. Returns undefined if not defined. */
	const getCacheKey = (key) => panelCacheKeys?.[key];
	/** Check if a panel has a defined (non-undefined) cache key. */
	const hasCacheKey = (key) => panelCacheKeys != null && key in panelCacheKeys && panelCacheKeys[key] !== void 0;
	/**
	* Determine if we should call renderPanel for this tab.
	*/
	const shouldCallRenderPanel = (tabKey, isActive) => {
		const cached = contentCacheRef.current.get(tabKey);
		if (!cached) return true;
		if (isActive) {
			if (hasCacheKey(tabKey)) {
				const currentCacheKey = getCacheKey(tabKey);
				return cached.cacheKey !== currentCacheKey;
			}
			return true;
		}
		return false;
	};
	const currentTabKeys = useMemo(() => new Set(parsedTabs.map((t) => t.key)), [parsedTabs]);
	useEffect(() => {
		for (const key of contentCacheRef.current.keys()) if (!currentTabKeys.has(key)) contentCacheRef.current.delete(key);
	}, [currentTabKeys]);
	return /* @__PURE__ */ jsx(Fragment$1, { children: parsedTabs.map((tab) => {
		const explicitPanel = explicitPanels.get(tab.key);
		const tabPrerender = explicitPanel?.prerender ?? tab.prerender;
		const tabKeepMounted = explicitPanel?.keepMounted ?? tab.keepMounted;
		const effectivePrerender = tabPrerender ?? prerender;
		const effectiveKeepMounted = tabKeepMounted ?? keepMounted;
		const isActive = state.selectedKey === tab.key;
		if (!shouldRenderPanel(isActive, visitedKeys.has(tab.key), effectivePrerender, effectiveKeepMounted)) {
			if (!effectiveKeepMounted && !effectivePrerender) contentCacheRef.current.delete(tab.key);
			return null;
		}
		let content;
		if (shouldCallRenderPanel(tab.key, isActive)) {
			content = renderPanel(tab.key);
			const currentCacheKey = getCacheKey(tab.key);
			contentCacheRef.current.set(tab.key, {
				content,
				cacheKey: currentCacheKey
			});
		} else content = contentCacheRef.current.get(tab.key)?.content ?? null;
		return /* @__PURE__ */ jsx(TabPanelRenderer, {
			tabKey: tab.key,
			state,
			content,
			prerender,
			keepMounted,
			tabPrerender,
			tabKeepMounted,
			visitedKeys,
			panelStyles: explicitPanel?.styles,
			qa: explicitPanel?.qa,
			qaVal: explicitPanel?.qaVal
		}, tab.key);
	}) });
}

//#endregion
export { CachedPanelRenderer, TabPanelRenderer };
//# sourceMappingURL=TabPanel.js.map