import { Fragment, jsx } from "react/jsx-runtime";
import { memo, useMemo } from "react";
import { useFindComponent } from "../../../schema-component/index.mjs";
import { SchemaInitializerItemContext } from "../context/index.mjs";
const SchemaInitializerChildren = (props)=>{
    const { children } = props;
    if (!children) return null;
    return /*#__PURE__*/ jsx(Fragment, {
        children: children.sort((a, b)=>(a.sort || 0) - (b.sort || 0)).map((item, index)=>/*#__PURE__*/ jsx(SchemaInitializerChild, {
                ...item
            }, item.key || (null == item ? void 0 : item.name) || index))
    });
};
const typeComponentMap = {
    item: 'SchemaInitializerItemInternal',
    itemGroup: 'SchemaInitializerItemGroupInternal',
    divider: 'SchemaInitializerDivider',
    subMenu: 'SchemaInitializerSubMenuInternal',
    actionModal: 'SchemaInitializerActionModalInternal'
};
const useChildrenDefault = ()=>void 0;
const useVisibleDefault = ()=>true;
const useComponentPropsDefault = ()=>void 0;
const SchemaInitializerChild = /*#__PURE__*/ memo((props)=>{
    const { type, Component, component, children, useVisible = useVisibleDefault, useChildren = useChildrenDefault, useComponentProps = useComponentPropsDefault, hideIfNoChildren, componentProps, sort: _unUse, ...others } = props;
    const useChildrenRes = useChildren();
    const useComponentPropsRes = useComponentProps();
    const findComponent = useFindComponent();
    const componentVal = Component || component;
    const isBuiltType = !componentVal && type && typeComponentMap[type];
    const componentChildren = useMemo(()=>{
        const res = [
            ...useChildrenRes || [],
            ...children || []
        ];
        return 0 === res.length ? void 0 : res;
    }, [
        useChildrenRes,
        children
    ]);
    const contextValue = useMemo(()=>({
            ...others,
            ...isBuiltType ? useComponentPropsRes : {},
            ...isBuiltType ? componentProps : {},
            children: componentChildren
        }), [
        others,
        isBuiltType,
        useComponentPropsRes,
        componentProps,
        componentChildren
    ]);
    const visibleResult = useVisible();
    if (!visibleResult) return null;
    if (!type && !componentVal) return null;
    const C = findComponent(isBuiltType ? typeComponentMap[type] : componentVal);
    if (!C) return null;
    if (hideIfNoChildren && Array.isArray(componentChildren) && 0 === componentChildren.length) return null;
    return /*#__PURE__*/ jsx(SchemaInitializerItemContext.Provider, {
        value: contextValue,
        children: /*#__PURE__*/ jsx(C, {
            ...componentProps,
            ...useComponentPropsRes,
            children: componentChildren
        })
    });
});
SchemaInitializerChild.displayName = 'SchemaInitializerChild';
export { SchemaInitializerChild, SchemaInitializerChildren };
