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

import { jsx as _jsx } from "react/jsx-runtime";
import { Children, cloneElement, createContext, useContext, useMemo, } from 'react';
import { mergeProps } from './mergeProps.js';
const INITIAL_VALUE = {};
let SlotContext = createContext(INITIAL_VALUE);
export function useSlotProps(props, defaultSlot) {
    let slot = props.slot || defaultSlot;
    let allSlots = useContext(SlotContext) ?? INITIAL_VALUE;
    let slotProps = allSlots[slot];
    return mergeProps(slotProps, props);
}
export function SlotProvider(props) {
    let parentSlots = useContext(SlotContext) ?? INITIAL_VALUE;
    let { slots = {}, children } = props;
    // Merge props for each slot from parent context and props
    let value = useMemo(() => Object.keys(parentSlots)
        .concat(Object.keys(slots))
        .reduce((o, p) => ({
        ...o,
        [p]: mergeProps(parentSlots[p] ?? {}, slots[p] ?? {}),
    }), {}), [parentSlots, slots]);
    return _jsx(SlotContext.Provider, { value: value, children: children });
}
export function ClearSlots(props) {
    const { children, ...otherProps } = props;
    let content = children;
    if (Children.toArray(children).length <= 1) {
        if (typeof children === 'function') {
            // need to know if the node is a string or something else that react can render that doesn't get props
            content = cloneElement(Children.only(children), otherProps);
        }
    }
    return (_jsx(SlotContext.Provider, { value: INITIAL_VALUE, children: content }));
}

        