/** @license MIT | @cube-dev/ui-kit v0.135.1 | Cube Dev Team */
import { mergeProps } from "./mergeProps.js";
import { Children, cloneElement, createContext, useContext, useMemo } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/utils/react/Slots.tsx
const INITIAL_VALUE = {};
let SlotContext = createContext(INITIAL_VALUE);
function useSlotProps(props, defaultSlot) {
	let slot = props.slot || defaultSlot;
	let slotProps = (useContext(SlotContext) ?? INITIAL_VALUE)[slot];
	return mergeProps(slotProps, props);
}
function SlotProvider(props) {
	let parentSlots = useContext(SlotContext) ?? INITIAL_VALUE;
	let { slots = {}, children } = props;
	let value = useMemo(() => Object.keys(parentSlots).concat(Object.keys(slots)).reduce((o, p) => ({
		...o,
		[p]: mergeProps(parentSlots[p] ?? {}, slots[p] ?? {})
	}), {}), [parentSlots, slots]);
	return /* @__PURE__ */ jsx(SlotContext.Provider, {
		value,
		children
	});
}
function ClearSlots(props) {
	const { children, ...otherProps } = props;
	let content = children;
	if (Children.toArray(children).length <= 1) {
		if (typeof children === "function") content = cloneElement(Children.only(children), otherProps);
	}
	return /* @__PURE__ */ jsx(SlotContext.Provider, {
		value: INITIAL_VALUE,
		children: content
	});
}

//#endregion
export { ClearSlots, SlotProvider, useSlotProps };
//# sourceMappingURL=Slots.js.map