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