import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
/* eslint-disable react-hooks/exhaustive-deps */
import * as React from 'react';
import DynamicComponent from '../../shared/dynamic-component/dynamic-component';
import {} from '../../types';
/**
 * Merge a list of context component definitions into a single component definition
 * [component1, component2, component3] => component1(component2(component3(children)))

 * @param contextComponents List of context components to merge
 * @param children Children to pass to the innermost component
 */
function mergeContext(contextComponents, children) {
    const [component, ...rest] = contextComponents;
    return Object.assign(Object.assign({}, component), { props: Object.assign(Object.assign({}, component.props), { children: rest.length === 0 ? children : _jsx(DynamicComponent, { component: mergeContext(rest, children) }) }) });
}
function DynamicContext(props) {
    const mergedContext = React.useMemo(() => {
        if (props.contextComponents.length === 0) {
            return null;
        }
        return mergeContext(props.contextComponents, props.children);
    }, [props.contextComponents]);
    if (!mergedContext) {
        return _jsx(_Fragment, { children: props.children });
    }
    return _jsx(DynamicComponent, { component: mergedContext });
}
export default DynamicContext;
//# sourceMappingURL=dynamic-context.js.map