import { Fragment, jsx } from "react/jsx-runtime";
import { memo, useEffect, useMemo, useRef } from "react";
import { useFieldComponentName } from "../../../common/useFieldComponentName.mjs";
import { useFindComponent } from "../../../schema-component/index.mjs";
import { SchemaSettingsActionModalItem, SchemaSettingsCascaderItem, SchemaSettingsDivider, SchemaSettingsItem, SchemaSettingsItemGroup, SchemaSettingsModalItem, SchemaSettingsPopupItem, SchemaSettingsRemove, SchemaSettingsSelectItem, SchemaSettingsSubMenu, SchemaSettingsSwitchItem, useSchemaSettings } from "../../../schema-settings/SchemaSettings.mjs";
import { SchemaSettingItemContext } from "../context/index.mjs";
const typeComponentMap = {
    item: SchemaSettingsItem,
    itemGroup: SchemaSettingsItemGroup,
    subMenu: SchemaSettingsSubMenu,
    divider: SchemaSettingsDivider,
    remove: SchemaSettingsRemove,
    select: SchemaSettingsSelectItem,
    cascader: SchemaSettingsCascaderItem,
    switch: SchemaSettingsSwitchItem,
    popup: SchemaSettingsPopupItem,
    actionModal: SchemaSettingsActionModalItem,
    modal: SchemaSettingsModalItem
};
const SchemaSettingsChildren = (props)=>{
    const { children } = props;
    const { visible } = useSchemaSettings();
    const firstVisible = useRef(false);
    const fieldComponentName = useFieldComponentName();
    useEffect(()=>{
        if (visible) firstVisible.current = true;
    }, [
        visible
    ]);
    if (!visible && !firstVisible.current) return null;
    if (!children || 0 === children.length) return null;
    return /*#__PURE__*/ jsx(Fragment, {
        children: children.sort((a, b)=>(a.sort || 0) - (b.sort || 0)).map((item)=>{
            const key = "".concat(fieldComponentName ? fieldComponentName + '-' : '').concat(item.name);
            return /*#__PURE__*/ jsx(SchemaSettingsChild, {
                ...item
            }, key);
        })
    });
};
const useChildrenDefault = ()=>void 0;
const useComponentPropsDefault = ()=>void 0;
const useVisibleDefault = ()=>true;
const InternalComponent = (param)=>{
    let { Component, componentProps, useComponentProps, children } = param;
    const useComponentPropsRes = useComponentProps();
    return /*#__PURE__*/ jsx(Component, {
        ...componentProps,
        ...useComponentPropsRes,
        children: children
    });
};
const SchemaSettingsChild = /*#__PURE__*/ memo((props)=>{
    const { useVisible = useVisibleDefault, useChildren = useChildrenDefault, useComponentProps = useComponentPropsDefault, type, Component, children, hideIfNoChildren = true, componentProps } = props;
    const useChildrenRes = useChildren();
    const findComponent = useFindComponent();
    const componentChildren = useMemo(()=>{
        const res = [
            ...useChildrenRes || [],
            ...children || []
        ];
        return 0 === res.length ? void 0 : res;
    }, [
        useChildrenRes,
        children
    ]);
    const visibleResult = useVisible();
    const ComponentValue = useMemo(()=>!Component && type && typeComponentMap[type] ? typeComponentMap[type] : Component, [
        type,
        Component
    ]);
    if (!visibleResult) return null;
    if (!type && !Component) return null;
    const C = findComponent(ComponentValue);
    if (!C) return null;
    if (hideIfNoChildren && Array.isArray(componentChildren) && 0 === componentChildren.length) return null;
    return /*#__PURE__*/ jsx(SchemaSettingItemContext.Provider, {
        value: props,
        children: /*#__PURE__*/ jsx(InternalComponent, {
            Component: C,
            useComponentProps: useComponentProps,
            componentProps: componentProps,
            children: Array.isArray(componentChildren) && componentChildren.length > 0 && /*#__PURE__*/ jsx(SchemaSettingsChildren, {
                children: componentChildren
            })
        })
    });
});
SchemaSettingsChild.displayName = 'SchemaSettingsChild';
export { SchemaSettingsChild, SchemaSettingsChildren };
