/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-e634e55 | Cube Dev Team */
import { extractLocalPredefinedStates } from "../states/index.js";
import { renderStyles } from "../pipeline/index.js";
import { CHUNK_NAMES } from "./definitions.js";

//#region src/tasty/chunks/renderChunk.ts
/**
* Chunk-specific style rendering.
*
* Renders styles for a specific chunk by filtering the styles object
* to only include relevant keys before passing to renderStyles.
*/
/**
* Render styles for a specific chunk.
*
* Creates a filtered styles object containing only the keys for this chunk,
* then delegates to the existing renderStyles function.
*
* IMPORTANT: Local predefined states (e.g., '@mobile': '@media(w < 600px)')
* are always included in the filtered styles, regardless of which chunk is
* being rendered. This ensures that state references like '@mobile' in any
* chunk can be properly resolved by the pipeline.
*
* @param styles - The full styles object
* @param chunkName - Name of the chunk being rendered
* @param styleKeys - Keys of styles belonging to this chunk
* @returns RenderResult with rules for this chunk
*/
function renderStylesForChunk(styles, chunkName, styleKeys) {
	if (styleKeys.length === 0) return {
		rules: [],
		className: ""
	};
	if (chunkName === CHUNK_NAMES.SUBCOMPONENTS) return renderSubcomponentsChunk(styles, styleKeys);
	const localPredefinedStates = extractLocalPredefinedStates(styles);
	const filteredStyles = {};
	for (const [key, value] of Object.entries(localPredefinedStates)) filteredStyles[key] = value;
	for (const key of styleKeys) {
		const value = styles[key];
		if (value !== void 0) filteredStyles[key] = value;
	}
	return renderStyles(filteredStyles);
}
/**
* Render the subcomponents chunk.
*
* Subcomponents (selectors like Label, &::before, etc.) contain nested
* style objects that need to be preserved in their entirety.
*
* @param styles - The full styles object
* @param selectorKeys - Keys of selectors in this chunk
* @returns RenderResult with rules for all subcomponents
*/
function renderSubcomponentsChunk(styles, selectorKeys) {
	const localPredefinedStates = extractLocalPredefinedStates(styles);
	const filteredStyles = {};
	for (const [key, value] of Object.entries(localPredefinedStates)) filteredStyles[key] = value;
	for (const key of selectorKeys) {
		const value = styles[key];
		if (value !== void 0) filteredStyles[key] = value;
	}
	if (styles.$ !== void 0) filteredStyles.$ = styles.$;
	return renderStyles(filteredStyles);
}

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