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__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_dom__ from "react-dom";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__ from "../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_SelectWithTitle_mjs__ from "../common/SelectWithTitle.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_useNiceDropdownHeight_mjs__ from "../common/useNiceDropdownHeight.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__ from "../data-source/data-source/DataSourceManagerProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__ from "../data-source/data-source/DataSourceProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__ from "../filter-provider/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__ from "../flag-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__ from "../hooks/useMenuItem.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_variable_DeclareVariable_mjs__ from "../modules/variable/DeclareVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_variable_useVariable_mjs__ from "../modules/variable/useVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_hooks_mjs__ from "../schema-component/antd/association-field/hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_filter_utilts_mjs__ from "../schema-component/antd/association-filter/utilts.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__ from "../schema-templates/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_BlockTemplate_mjs__ from "../schema-templates/BlockTemplate.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataTemplates_index_mjs__ from "./DataTemplates/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__EnableChildCollections_index_mjs__ from "./EnableChildCollections/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__EnableChildCollections_DynamicComponent_mjs__ from "./EnableChildCollections/DynamicComponent.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__LinkageRules_index_mjs__ from "./LinkageRules/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__LinkageRules_action_hooks_mjs__ from "./LinkageRules/action-hooks.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      .ant-dropdown-menu-item-group-list {\n        max-height: 400px;\n        overflow-y: auto;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      // screen > 576px\n      @media (min-width: 576px) {\n        min-width: 520px;\n      }\n\n      // screen <= 576px\n      @media (max-width: 576px) {\n        min-width: 320px;\n      }\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const SchemaSettingsContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
SchemaSettingsContext.displayName = 'SchemaSettingsContext';
function useSchemaSettings() {
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SchemaSettingsContext);
}
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        menu: css(_templateObject()),
        modal: css(_templateObject1())
    };
});
const SchemaSettingsProvider = (props)=>{
    const { children, fieldSchema, ...others } = props;
    const { getTemplateBySchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const { name } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const template = getTemplateBySchema(fieldSchema);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsContext.Provider, {
        value: {
            collectionName: name,
            template,
            fieldSchema,
            ...others
        },
        children: children
    });
};
const SchemaSettingsDropdown = (props)=>{
    const { title, dn, ...others } = props;
    const { styles } = useStyles();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { Component, getMenuItems } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useMenuItem)();
    const [, startTransition] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useTransition)();
    const dropdownMaxHeight = (0, __WEBPACK_EXTERNAL_MODULE__common_useNiceDropdownHeight_mjs__.useNiceDropdownMaxHeight)([
        visible
    ]);
    const changeMenu = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((nextOpen, info)=>{
        if ('trigger' === info.source || nextOpen) startTransition(()=>{
            setVisible(nextOpen);
        });
    }, []);
    const items = getMenuItems(()=>props.children);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(SchemaSettingsProvider, {
        visible: visible,
        setVisible: setVisible,
        dn: dn,
        ...others,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Dropdown, {
                open: visible,
                onOpenChange: changeMenu,
                overlayClassName: styles.menu,
                menu: {
                    items,
                    style: {
                        maxHeight: dropdownMaxHeight,
                        overflowY: 'auto'
                    }
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    "data-testid": props['data-testid'],
                    children: 'string' == typeof title ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                        children: title
                    }) : title
                })
            })
        ]
    });
};
const SchemaSettingsTemplate = function(props) {
    const { componentName, collectionName, resourceName, needRender } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const { dn, setVisible, template, fieldSchema } = useSchemaSettings();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { dn: tdn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_BlockTemplate_mjs__.useBlockTemplateContext)();
    const { saveAsTemplate, copyTemplateSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useGlobalTheme)();
    if (!collectionName && !needRender) return null;
    if (template) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: "Convert reference to duplicate",
        onClick: async ()=>{
            const schema = await copyTemplateSchema(template);
            const removed = tdn.removeWithoutEmit();
            tdn.insertAfterEnd(schema, {
                async onSuccess () {
                    await api.request({
                        url: "/uiSchemas:remove/".concat(removed['x-uid'])
                    });
                }
            });
        },
        children: t('Convert reference to duplicate')
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: "Save as template",
        onClick: async ()=>{
            setVisible(false);
            const collection = collectionName && getCollection(collectionName);
            const values = await (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.FormDialog)(t('Save as template'), ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                    layout: 'vertical',
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                        components: {
                            Input: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.Input,
                            FormItem: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem
                        },
                        schema: {
                            type: 'object',
                            properties: {
                                name: {
                                    title: t('Template name'),
                                    required: true,
                                    default: collection ? "".concat(compile((null == collection ? void 0 : collection.title) || (null == collection ? void 0 : collection.name)), "_").concat(t(componentName)) : t(componentName),
                                    'x-decorator': 'FormItem',
                                    'x-component': 'Input'
                                }
                            }
                        }
                    })
                }), theme).open({});
            const sdn = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.createDesignable)({
                t,
                api,
                refresh: dn.refresh.bind(dn),
                current: fieldSchema.parent
            });
            sdn.loadAPIClientEvents();
            const { key } = await saveAsTemplate({
                collectionName,
                resourceName,
                componentName,
                dataSourceKey: collection.dataSource,
                name: values.name,
                uid: fieldSchema['x-uid']
            });
            sdn.removeWithoutEmit(fieldSchema);
            sdn.insertBeforeEnd({
                type: 'void',
                'x-component': 'BlockTemplate',
                'x-component-props': {
                    templateId: key
                }
            });
        },
        children: t('Save as template')
    });
};
const findGridSchema = (fieldSchema)=>fieldSchema.reduceProperties((buf, s)=>{
        if ('FormV2' === s['x-component'] || 'Details' === s['x-component']) {
            const f = s.reduceProperties((buf, s)=>{
                if ('Grid' === s['x-component'] || 'BlockTemplate' === s['x-component']) return s;
                return buf;
            }, null);
            if (f) return f;
        }
        return buf;
    }, null);
const findBlockTemplateSchema = (fieldSchema)=>fieldSchema.reduceProperties((buf, s)=>{
        if ('FormV2' === s['x-component'] || 'Details' === s['x-component']) {
            const f = s.reduceProperties((buf, s)=>{
                if ('BlockTemplate' === s['x-component']) return s;
                return buf;
            }, null);
            if (f) return f;
        }
        return buf;
    }, null);
const SchemaSettingsFormItemTemplate = function(props) {
    const { insertAdjacentPosition = 'beforeEnd', componentName, collectionName, resourceName } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const { dn, setVisible, template, fieldSchema } = useSchemaSettings();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { saveAsTemplate, copyTemplateSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useGlobalTheme)();
    if (!collectionName) return null;
    if (template) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: "Convert reference to duplicate",
        onClick: async ()=>{
            const schema = await copyTemplateSchema(template);
            const templateSchema = findBlockTemplateSchema(fieldSchema);
            const sdn = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.createDesignable)({
                t,
                api,
                refresh: dn.refresh.bind(dn),
                current: templateSchema.parent
            });
            sdn.loadAPIClientEvents();
            sdn.removeWithoutEmit(templateSchema);
            sdn.insertAdjacent(insertAdjacentPosition, schema, {
                async onSuccess () {
                    await api.request({
                        url: "/uiSchemas:remove/".concat(templateSchema['x-uid'])
                    });
                }
            });
            fieldSchema['x-template-key'] = null;
            await api.request({
                url: "uiSchemas:patch",
                method: 'post',
                data: {
                    'x-uid': fieldSchema['x-uid'],
                    'x-template-key': null
                }
            });
            dn.refresh();
        },
        children: t('Convert reference to duplicate')
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: "Save as block template",
        onClick: async ()=>{
            setVisible(false);
            const collection = collectionName && getCollection(collectionName);
            const gridSchema = findGridSchema(fieldSchema);
            const values = await (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.FormDialog)(t('Save as template'), ()=>{
                const componentTitle = {
                    FormItem: t('Form'),
                    ReadPrettyFormItem: t('Details')
                };
                return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                    layout: 'vertical',
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                        components: {
                            Input: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.Input,
                            FormItem: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormItem
                        },
                        schema: {
                            type: 'object',
                            properties: {
                                name: {
                                    title: t('Template name'),
                                    required: true,
                                    default: collection ? "".concat(compile((null == collection ? void 0 : collection.title) || (null == collection ? void 0 : collection.name)), "_").concat(t(componentTitle[componentName] || componentName)) : t(componentTitle[componentName] || componentName),
                                    'x-decorator': 'FormItem',
                                    'x-component': 'Input'
                                }
                            }
                        }
                    })
                });
            }, theme).open({});
            const sdn = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.createDesignable)({
                t,
                api,
                refresh: dn.refresh.bind(dn),
                current: gridSchema.parent
            });
            sdn.loadAPIClientEvents();
            const { key } = await saveAsTemplate({
                collectionName,
                resourceName,
                componentName,
                dataSourceKey: collection.dataSource,
                name: values.name,
                uid: gridSchema['x-uid']
            });
            sdn.removeWithoutEmit(gridSchema);
            sdn.insertAdjacent(insertAdjacentPosition, {
                type: 'void',
                'x-component': 'BlockTemplate',
                'x-component-props': {
                    templateId: key
                }
            });
            fieldSchema['x-template-key'] = key;
            await api.request({
                url: "uiSchemas:patch",
                method: 'post',
                data: {
                    'x-uid': fieldSchema['x-uid'],
                    'x-template-key': key
                }
            });
        },
        children: t('Save as block template')
    });
};
const SchemaSettingsItem = (props)=>{
    const { eventKey, title } = props;
    const { pushMenuItem } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useCollectMenuItems)();
    const { collectMenuItem } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useCollectMenuItem)();
    const { name } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSchemaSettingsItem)();
    const item = {
        key: title,
        ...__WEBPACK_EXTERNAL_MODULE_lodash__["default"].omit(props, [
            'children',
            'name'
        ]),
        eventKey: eventKey,
        onClick: (info)=>{
            var _props_onClick;
            info.domEvent.preventDefault();
            info.domEvent.stopPropagation();
            null == props || null === (_props_onClick = props.onClick) || void 0 === _props_onClick || _props_onClick.call(props, info);
        },
        style: {
            minWidth: 120
        },
        label: props.children || props.title,
        title: props.title
    };
    null == pushMenuItem || pushMenuItem(item);
    null == collectMenuItem || collectMenuItem(item);
    return null;
};
const SchemaSettingsItemGroup = (props)=>{
    const { Component, getMenuItems } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useMenuItem)();
    const { pushMenuItem } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useCollectMenuItems)();
    const key = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(), []);
    const item = {
        key,
        type: 'group',
        title: props.title,
        label: props.title,
        children: getMenuItems(()=>props.children)
    };
    pushMenuItem(item);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {});
};
const SchemaSettingsSubMenu = function(props) {
    const { Component, getMenuItems } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useMenuItem)();
    const { pushMenuItem } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useCollectMenuItems)();
    const key = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(), []);
    const item = {
        key,
        label: props.title,
        title: props.title,
        children: getMenuItems(()=>props.children)
    };
    pushMenuItem(item);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {});
};
const SchemaSettingsDivider = function() {
    const { pushMenuItem } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useMenuItem_mjs__.useCollectMenuItems)();
    const key = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(), []);
    const item = {
        key,
        type: 'divider'
    };
    pushMenuItem(item);
    return null;
};
const SchemaSettingsRemove = (props)=>{
    const { icon, confirm, removeParentsIfNoChildren, breakRemoveOn } = props;
    const { dn, template } = useSchemaSettings();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_BlockTemplate_mjs__.useBlockTemplateContext)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { modal } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { removeActiveFieldName } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useFormActiveFields)() || {};
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: "Delete",
        eventKey: "remove",
        icon: icon ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.Icon, {
            style: {
                color: '#eb5050'
            },
            type: icon
        }) : null,
        onClick: ()=>{
            modal.confirm({
                title: t('Delete block'),
                content: t('Are you sure you want to delete it?'),
                ...confirm,
                async onOk () {
                    var _confirm_onOk;
                    const options = {
                        removeParentsIfNoChildren,
                        breakRemoveOn
                    };
                    if (null == field ? void 0 : field.required) {
                        field.required = false;
                        fieldSchema['required'] = false;
                    }
                    if (template && template.uid === fieldSchema['x-uid'] && (null == ctx ? void 0 : ctx.dn)) await (null == ctx ? void 0 : ctx.dn.remove(null, options));
                    else await dn.remove(null, options);
                    await (null == confirm ? void 0 : null === (_confirm_onOk = confirm.onOk) || void 0 === _confirm_onOk ? void 0 : _confirm_onOk.call(confirm));
                    delete form.values[fieldSchema.name];
                    null == removeActiveFieldName || removeActiveFieldName(fieldSchema.name);
                    if ((null == field ? void 0 : field.setInitialValue) && (null == field ? void 0 : field.reset)) {
                        field.setInitialValue(null);
                        field.reset();
                    }
                }
            });
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
            style: {
                color: '#eb5050'
            },
            children: t('Delete')
        })
    });
};
const SchemaSettingsConnectDataBlocks = (props)=>{
    const { type, emptyDescription } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const { inProvider } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useFilterBlock)();
    const dataBlocks = (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.useSupportedBlocks)(type);
    let { targets = [], uid } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.findFilterTargets)(fieldSchema);
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const { getAllCollectionsInheritChain } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    if (!inProvider) return null;
    const Content = dataBlocks.map((block)=>{
        const title = "".concat(compile(block.collection.title), " #").concat(block.uid.slice(0, 4));
        const onHover = ()=>{
            const originalScrollPosition = window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
            const dom = block.dom;
            const designer = dom.querySelector('.general-schema-designer');
            if (designer) designer.style.display = 'block';
            dom.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';
            setTimeout(()=>{
                dom.scrollIntoView({
                    behavior: 'smooth',
                    block: 'center'
                });
            }, 1000);
            setTimeout(()=>{
                window.scrollTo({
                    top: originalScrollPosition,
                    behavior: 'smooth'
                });
            }, 2000);
        };
        const onLeave = ()=>{
            const dom = block.dom;
            const designer = dom.querySelector('.general-schema-designer');
            if (designer) designer.style.display = null;
            dom.style.boxShadow = 'none';
        };
        if ((0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.isSameCollection)(block.collection, collection)) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsSwitchItem, {
            title: title,
            checked: targets.some((target)=>target.uid === block.uid),
            onChange: (checked)=>{
                if (checked) targets.push({
                    uid: block.uid
                });
                else {
                    targets = targets.filter((target)=>target.uid !== block.uid);
                    block.clearFilter(uid);
                }
                (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.updateFilterTargets)(fieldSchema, targets);
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: uid,
                        'x-filter-targets': targets
                    }
                }).catch(__WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.error);
                dn.refresh();
            },
            onMouseLeave: onLeave,
            onMouseUp: onHover
        }, block.uid);
        const target = targets.find((target)=>target.uid === block.uid);
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsSelectItem, {
            title: title,
            value: (null == target ? void 0 : target.field) || '',
            options: [
                ...(0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.getSupportFieldsByAssociation)(getAllCollectionsInheritChain(collection.name), block).map((field)=>({
                        label: compile(field.uiSchema.title) || field.name,
                        value: "".concat(field.name, ".").concat((0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_filter_utilts_mjs__.getTargetKey)(field))
                    })),
                ...(0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.getSupportFieldsByForeignKey)(collection, block).map((field)=>({
                        label: "".concat(compile(field.uiSchema.title) || field.name, " [").concat(t('Foreign key'), "]"),
                        value: field.name
                    })),
                {
                    label: t('Unconnected'),
                    value: ''
                }
            ],
            onChange: (value)=>{
                if ('' === value) {
                    targets = targets.filter((target)=>target.uid !== block.uid);
                    block.clearFilter(uid);
                } else {
                    targets = targets.filter((target)=>target.uid !== block.uid);
                    targets.push({
                        uid: block.uid,
                        field: value
                    });
                }
                (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.updateFilterTargets)(fieldSchema, targets);
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: uid,
                        'x-filter-targets': targets
                    }
                });
                dn.refresh();
            },
            onMouseEnter: onHover,
            onMouseLeave: onLeave
        }, block.uid);
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsSubMenu, {
        title: t('Connect data blocks'),
        children: Content.length ? Content : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
            title: "empty",
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Empty, {
                style: {
                    width: 160,
                    padding: '0 1em'
                },
                description: emptyDescription,
                image: __WEBPACK_EXTERNAL_MODULE_antd__.Empty.PRESENTED_IMAGE_SIMPLE
            })
        })
    });
};
const SchemaSettingsSelectItem = (props)=>{
    const { title, options, value, onChange, fieldNames, ...others } = props;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: title,
        ...others,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__common_SelectWithTitle_mjs__.SelectWithTitle, {
            title,
            defaultValue: value,
            onChange,
            options,
            fieldNames
        })
    });
};
const SchemaSettingsCascaderItem = (props)=>{
    const { title, options, value, onChange, ...others } = props;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: title,
        ...others,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            style: {
                alignItems: 'center',
                display: 'flex',
                justifyContent: 'space-between'
            },
            children: [
                title,
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Cascader, {
                    variant: "borderless",
                    defaultValue: value,
                    onChange: onChange,
                    options: options,
                    style: {
                        textAlign: 'right',
                        minWidth: 100
                    },
                    multiple: !!props.multiple,
                    ...props
                })
            ]
        })
    });
};
const SchemaSettingsSwitchItem = (props)=>{
    const { title, onChange, ...others } = props;
    const [checked, setChecked] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(!!props.checked);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: title,
        ...others,
        onClick: ()=>{
            null == onChange || onChange(!checked);
            setChecked(!checked);
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            style: {
                alignItems: 'center',
                display: 'flex',
                justifyContent: 'space-between'
            },
            children: [
                title,
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Switch, {
                    size: 'small',
                    checked: checked,
                    style: {
                        marginLeft: 32
                    }
                })
            ]
        })
    });
};
const SchemaSettingsPopupItem = (props)=>{
    const { schema, ...others } = props;
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SchemaSettingsContext);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ActionContextProvider, {
        value: {
            visible,
            setVisible
        },
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
                title: props.title,
                ...others,
                onClick: ()=>{
                    ctx.setVisible(false);
                    setVisible(true);
                },
                children: props.children || props.title
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                schema: {
                    name: (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(),
                    ...schema
                }
            })
        ]
    });
};
const SchemaSettingsActionModalItem = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].memo((props)=>{
    const { title, onSubmit, initialValues, beforeOpen, initialSchema, schema, modalTip, components, scope, ...others } = props;
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schemaUid, setSchemaUid] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(props.uid);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SchemaSettingsContext);
    const { dn } = useSchemaSettings();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const upLevelActiveFields = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useFormActiveFields)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            initialValues: (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(initialValues),
            values: (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(initialValues)
        }), [
        initialValues
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        form.setInitialValues((0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(initialValues));
    }, [
        JSON.stringify(initialValues || {})
    ]);
    const cancelHandler = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        setVisible(false);
        form.reset();
    }, [
        form
    ]);
    const submitHandler = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async ()=>{
        await form.submit();
        null == onSubmit || onSubmit((0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(form.values));
        setVisible(false);
    }, [
        form,
        onSubmit
    ]);
    const openAssignedFieldValueHandler = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async ()=>{
        if (!schemaUid && (null == initialSchema ? void 0 : initialSchema['x-uid'])) {
            fieldSchema['x-action-settings'].schemaUid = initialSchema['x-uid'];
            dn.emit('patch', {
                schema: fieldSchema
            });
            await api.resource('uiSchemas').insert({
                values: initialSchema
            });
            setSchemaUid(initialSchema['x-uid']);
        }
        if ('function' == typeof beforeOpen) null == beforeOpen || beforeOpen();
        ctx.setVisible(false);
        setVisible(true);
    }, [
        api,
        ctx,
        dn,
        fieldSchema,
        initialSchema,
        schemaUid
    ]);
    const onKeyDown = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((e)=>e.stopPropagation(), []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
                title: compile(title),
                ...others,
                onClick: openAssignedFieldValueHandler,
                onKeyDown: onKeyDown,
                children: props.children || props.title
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_dom__.createPortal)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Modal, {
                width: '50%',
                title: compile(title),
                ...others,
                destroyOnClose: true,
                open: visible,
                onCancel: cancelHandler,
                footer: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                            onClick: cancelHandler,
                            children: t('Cancel')
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                            type: "primary",
                            onClick: submitHandler,
                            children: t('Submit')
                        })
                    ]
                }),
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.FormActiveFieldsProvider, {
                    name: "form",
                    getActiveFieldsName: null == upLevelActiveFields ? void 0 : upLevelActiveFields.getActiveFieldsName,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.FormProvider, {
                        form: form,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                            layout: 'vertical',
                            children: [
                                modalTip && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Alert, {
                                    message: modalTip
                                }),
                                modalTip && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("br", {}),
                                visible && schemaUid && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.RemoteSchemaComponent, {
                                    noForm: true,
                                    components: components,
                                    scope: scope,
                                    uid: schemaUid
                                }),
                                visible && schema && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                                    components: components,
                                    scope: scope,
                                    schema: schema
                                })
                            ]
                        })
                    })
                })
            }), document.body)
        ]
    });
});
SchemaSettingsActionModalItem.displayName = 'SchemaSettingsActionModalItem';
const SchemaSettingsModalItem = (props)=>{
    const { hidden, title, components, scope, effects, onSubmit, asyncGetInitialValues, initialValues, width = 'fit-content', ...others } = props;
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaOptionsContext);
    options.components = {
        ...options.components,
        Input: __WEBPACK_EXTERNAL_MODULE__index_mjs__.Input,
        'Input.TextArea': __WEBPACK_EXTERNAL_MODULE__index_mjs__.Input.TextArea,
        'Radio.Group': __WEBPACK_EXTERNAL_MODULE__index_mjs__.Radio.Group,
        'Checkbox.Group': __WEBPACK_EXTERNAL_MODULE__index_mjs__.Checkbox.Group,
        DatePicker: __WEBPACK_EXTERNAL_MODULE__index_mjs__.DatePicker,
        Select: __WEBPACK_EXTERNAL_MODULE__index_mjs__.Select,
        AlternativeAssociationSelect: void 0,
        Cascader: __WEBPACK_EXTERNAL_MODULE__index_mjs__.Cascader
    };
    const { styles } = useStyles();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const apiClient = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useGlobalTheme)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useBlockRequestContext)();
    const upLevelActiveFields = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useFormActiveFields)();
    const { locale } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider.ConfigContext);
    const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__.useDataSourceManager)();
    const dataSourceKey = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__.useDataSourceKey)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionRecord)();
    const { association } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDataBlockProps)() || {};
    const formCtx = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const app = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useApp)();
    const { formValue: subFormValue, collection: subFormCollection } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_hooks_mjs__.useSubFormValue)();
    const popupRecordVariable = (0, __WEBPACK_EXTERNAL_MODULE__modules_variable_useVariable_mjs__.useVariable)('$nPopupRecord');
    if (hidden) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsItem, {
        title: title,
        ...others,
        onClick: async ()=>{
            const values = asyncGetInitialValues ? await asyncGetInitialValues() : initialValues;
            const schema = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isFunction(props.schema) ? props.schema() : props.schema;
            (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.FormDialog)({
                title: schema.title || title,
                width
            }, ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__modules_variable_DeclareVariable_mjs__.DeclareVariable, {
                    name: "$nPopupRecord",
                    title: popupRecordVariable.title,
                    value: popupRecordVariable.value,
                    collection: popupRecordVariable.collection,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ApplicationContext.Provider, {
                        value: app,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CollectionRecordProvider, {
                            record: record,
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.FormBlockContext.Provider, {
                                value: formCtx,
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_hooks_mjs__.SubFormProvider, {
                                    value: {
                                        value: subFormValue,
                                        collection: subFormCollection
                                    },
                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.FormActiveFieldsProvider, {
                                        name: "form",
                                        getActiveFieldsName: null == upLevelActiveFields ? void 0 : upLevelActiveFields.getActiveFieldsName,
                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Router, {
                                            location: location,
                                            navigator: null,
                                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.BlockRequestContext_deprecated.Provider, {
                                                value: ctx,
                                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.DataSourceApplicationProvider, {
                                                    dataSourceManager: dm,
                                                    dataSource: dataSourceKey,
                                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.AssociationOrCollectionProvider, {
                                                        allowNull: true,
                                                        collection: collection.name,
                                                        association: association,
                                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponentOptions, {
                                                            scope: options.scope,
                                                            components: options.components,
                                                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                                                                layout: 'vertical',
                                                                className: styles.modal,
                                                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.APIClientProvider, {
                                                                    apiClient: apiClient,
                                                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider, {
                                                                        locale: locale,
                                                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponent, {
                                                                            components: components,
                                                                            scope: scope,
                                                                            schema: schema
                                                                        })
                                                                    })
                                                                })
                                                            })
                                                        })
                                                    })
                                                })
                                            })
                                        })
                                    })
                                })
                            })
                        })
                    })
                }), theme).open({
                initialValues: values,
                effects
            }).then((values)=>{
                onSubmit(values);
                return values;
            }).catch((err)=>{
                console.error(err);
            });
        },
        children: props.children || props.title
    });
};
const SchemaSettingsBlockTitleItem = function() {
    var _fieldSchema_xcomponentprops;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsModalItem, {
        title: t('Edit block title'),
        schema: {
            type: 'object',
            title: t('Edit block title'),
            properties: {
                title: {
                    title: t('Block title'),
                    type: 'string',
                    default: null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['title'],
                    'x-decorator': 'FormItem',
                    'x-component': 'Input'
                }
            }
        },
        onSubmit: (param)=>{
            let { title } = param;
            const componentProps = fieldSchema['x-component-props'] || {};
            componentProps.title = title;
            fieldSchema['x-component-props'] = componentProps;
            field.componentProps.title = title;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-component-props': fieldSchema['x-component-props']
                }
            });
            dn.refresh();
        }
    });
};
const SchemaSettingsDefaultSortingRules = function(props) {
    const { path = 'x-component-props.params.sort' } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const title = props.title || t('Set default sorting rules');
    const { name } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const defaultSort = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.get)(fieldSchema, path) || [];
    const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>item.startsWith('-') ? {
            field: item.substring(1),
            direction: 'desc'
        } : {
            field: item,
            direction: 'asc'
        });
    const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSortFields)(props.name || name);
    const onSubmit = async (param)=>{
        let { sort } = param;
        var _props_onSubmitAfter;
        if (null == props ? void 0 : props.onSubmit) return props.onSubmit({
            sort
        });
        const value = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
        (0, __WEBPACK_EXTERNAL_MODULE_lodash__.set)(field, path.replace('x-component-props', 'componentProps').replace('x-decorator-props', 'decoratorProps'), value);
        (0, __WEBPACK_EXTERNAL_MODULE_lodash__.set)(fieldSchema, path, value);
        await dn.emit('patch', {
            schema: fieldSchema
        });
        return null === (_props_onSubmitAfter = props.onSubmitAfter) || void 0 === _props_onSubmitAfter ? void 0 : _props_onSubmitAfter.call(props);
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsModalItem, {
        title: title,
        components: {
            ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayItems
        },
        schema: {
            type: 'object',
            title,
            properties: {
                sort: {
                    type: 'array',
                    default: sort,
                    'x-component': 'ArrayItems',
                    'x-decorator': 'FormItem',
                    items: {
                        type: 'object',
                        properties: {
                            space: {
                                type: 'void',
                                'x-component': 'Space',
                                properties: {
                                    sort: {
                                        type: 'void',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'ArrayItems.SortHandle'
                                    },
                                    field: {
                                        type: 'string',
                                        enum: sortFields,
                                        required: true,
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Select',
                                        'x-component-props': {
                                            style: {
                                                width: 260
                                            },
                                            optionLabelProp: 'fullLabel'
                                        }
                                    },
                                    direction: {
                                        type: 'string',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Radio.Group',
                                        'x-component-props': {
                                            optionType: 'button'
                                        },
                                        enum: [
                                            {
                                                label: t('ASC'),
                                                value: 'asc'
                                            },
                                            {
                                                label: t('DESC'),
                                                value: 'desc'
                                            }
                                        ]
                                    },
                                    remove: {
                                        type: 'void',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'ArrayItems.Remove'
                                    }
                                }
                            }
                        }
                    },
                    properties: {
                        add: {
                            type: 'void',
                            title: t('Add sort field'),
                            'x-component': 'ArrayItems.Addition'
                        }
                    }
                }
            }
        },
        onSubmit: onSubmit
    });
};
const SchemaSettingsLinkageRules = function(props) {
    const { collectionName } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { getTemplateById } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRecord)();
    const { type: formBlockType } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockType)();
    const type = (null == props ? void 0 : props.type) || [
        'Action',
        'Action.Link'
    ].includes(fieldSchema['x-component']) ? 'button' : 'field';
    const gridSchema = findGridSchema(fieldSchema) || fieldSchema;
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            type: 'object',
            title: t('Linkage rules'),
            properties: {
                fieldReaction: {
                    'x-component': __WEBPACK_EXTERNAL_MODULE__LinkageRules_index_mjs__.FormLinkageRules,
                    'x-use-component-props': ()=>{
                        const options = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useLinkageCollectionFilterOptions)(collectionName);
                        const result = {
                            options,
                            defaultValues: (null == gridSchema ? void 0 : gridSchema['x-linkage-rules']) || (null == fieldSchema ? void 0 : fieldSchema['x-linkage-rules']),
                            type,
                            linkageOptions: (0, __WEBPACK_EXTERNAL_MODULE__LinkageRules_action_hooks_mjs__.useLinkageCollectionFieldOptions)(collectionName),
                            collectionName,
                            form,
                            variables,
                            localVariables,
                            record,
                            formBlockType
                        };
                        return result;
                    }
                }
            }
        }), [
        collectionName,
        fieldSchema,
        form,
        gridSchema,
        localVariables,
        record,
        t,
        type,
        variables
    ]);
    const components = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
            FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
        }), []);
    const onSubmit = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((v)=>{
        var _gridSchema_xcomponentprops;
        const rules = [];
        for (const rule of v.fieldReaction.rules)rules.push(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].pickBy(rule, __WEBPACK_EXTERNAL_MODULE_lodash__["default"].identity));
        const templateId = 'BlockTemplate' === gridSchema['x-component'] && (null === (_gridSchema_xcomponentprops = gridSchema['x-component-props']) || void 0 === _gridSchema_xcomponentprops ? void 0 : _gridSchema_xcomponentprops.templateId);
        const uid = templateId && getTemplateById(templateId).uid || gridSchema['x-uid'];
        const schema = {
            ['x-uid']: uid
        };
        gridSchema['x-linkage-rules'] = rules;
        schema['x-linkage-rules'] = rules;
        dn.emit('patch', {
            schema
        });
        dn.refresh();
    }, [
        dn,
        getTemplateById,
        gridSchema
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsModalItem, {
        title: t('Linkage rules'),
        components: components,
        width: 770,
        schema: schema,
        onSubmit: onSubmit
    });
};
const useDataTemplates = (schema)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    if (schema) return {
        templateData: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(schema['x-data-templates'])
    };
    const formSchema = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.findFormBlock)(fieldSchema) || fieldSchema;
    return {
        templateData: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(null == formSchema ? void 0 : formSchema['x-data-templates'])
    };
};
const SchemaSettingsDataTemplates = function(props) {
    const designerCtx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponentContext);
    const { collectionName } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const formSchema = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.findFormBlock)(fieldSchema) || fieldSchema;
    const { templateData } = useDataTemplates();
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            type: 'object',
            title: t('Form data templates'),
            properties: {
                fieldReaction: {
                    'x-decorator': (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.FlagProvider, {
                            ...props,
                            isInFormDataTemplate: true
                        }),
                    'x-component': __WEBPACK_EXTERNAL_MODULE__DataTemplates_index_mjs__.FormDataTemplates,
                    'x-use-component-props': ()=>({
                            defaultValues: templateData,
                            collectionName
                        }),
                    'x-component-props': {
                        designerCtx,
                        formSchema
                    }
                }
            }
        }), [
        templateData
    ]);
    const onSubmit = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((v)=>{
        const data = {
            ...formSchema['x-data-templates'] || {},
            ...v.fieldReaction
        };
        data.items.forEach((item)=>{
            item.fields = Array.isArray(item.fields) ? item.fields : item.fields.checked;
        });
        const schema = {
            ['x-uid']: formSchema['x-uid'],
            ['x-data-templates']: data
        };
        formSchema['x-data-templates'] = data;
        dn.emit('patch', {
            schema
        });
        dn.refresh();
    }, []);
    const title = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>t('Form data templates'), []);
    const components = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
            FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
        }), []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsModalItem, {
        title: title,
        components: components,
        width: 770,
        schema: schema,
        onSubmit: onSubmit
    });
};
function SchemaSettingsEnableChildCollections(props) {
    var _fieldSchema_parent, _fieldSchema_parent1, _fieldSchema_xcomponentprops;
    const { collectionName } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const allowAddToCurrent = null == fieldSchema ? void 0 : fieldSchema['x-allow-add-to-current'];
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useBlockRequestContext)();
    const collectionField = getCollectionJoinField(null == fieldSchema ? void 0 : null === (_fieldSchema_parent = fieldSchema.parent) || void 0 === _fieldSchema_parent ? void 0 : _fieldSchema_parent['x-collection-field']) || {};
    const isAssocationAdd = (null == fieldSchema ? void 0 : null === (_fieldSchema_parent1 = fieldSchema.parent) || void 0 === _fieldSchema_parent1 ? void 0 : _fieldSchema_parent1['x-component']) === 'CollectionField';
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsModalItem, {
        title: t('Enable child collections'),
        components: {
            ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayItems,
            FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
        },
        scope: {
            isAssocationAdd
        },
        schema: {
            type: 'object',
            title: t('Enable child collections'),
            properties: {
                enableChildren: {
                    'x-component': __WEBPACK_EXTERNAL_MODULE__EnableChildCollections_index_mjs__.EnableChildCollections,
                    'x-use-component-props': ()=>({
                            defaultValues: null == fieldSchema ? void 0 : fieldSchema['x-enable-children'],
                            collectionName
                        })
                },
                allowAddToCurrent: {
                    type: 'boolean',
                    'x-content': "{{t('Allow adding records to the current collection')}}",
                    'x-decorator': 'FormItem',
                    'x-component': 'Checkbox',
                    default: void 0 === allowAddToCurrent || allowAddToCurrent
                },
                linkageFromForm: {
                    type: 'string',
                    title: "{{t('Linkage with form fields')}}",
                    'x-visible': '{{isAssocationAdd}}',
                    'x-decorator': 'FormItem',
                    'x-component': __WEBPACK_EXTERNAL_MODULE__EnableChildCollections_DynamicComponent_mjs__.ChildDynamicComponent,
                    'x-component-props': {
                        rootCollection: ctx.props.collection || ctx.props.resource,
                        collectionField
                    },
                    default: null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['linkageFromForm']
                }
            }
        },
        onSubmit: (v)=>{
            const enableChildren = [];
            for (const item of v.enableChildren.childrenCollections)enableChildren.push(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].pickBy(item, __WEBPACK_EXTERNAL_MODULE_lodash__["default"].identity));
            const uid = fieldSchema['x-uid'];
            const schema = {
                ['x-uid']: uid
            };
            fieldSchema['x-enable-children'] = enableChildren;
            fieldSchema['x-allow-add-to-current'] = v.allowAddToCurrent;
            fieldSchema['x-component-props'] = {
                ...fieldSchema['x-component-props'],
                component: 'CreateRecordAction',
                linkageFromForm: null == v ? void 0 : v.linkageFromForm
            };
            schema['x-enable-children'] = enableChildren;
            schema['x-allow-add-to-current'] = v.allowAddToCurrent;
            schema['x-component-props'] = {
                ...fieldSchema['x-component-props'],
                component: 'CreateRecordAction',
                linkageFromForm: null == v ? void 0 : v.linkageFromForm
            };
            field.componentProps['linkageFromForm'] = v.linkageFromForm;
            dn.emit('patch', {
                schema
            });
            dn.refresh();
        }
    });
}
const findParentFieldSchema = (fieldSchema)=>{
    let parent = fieldSchema.parent;
    while(parent){
        if ('CollectionField' === parent['x-component']) return parent;
        parent = parent.parent;
    }
};
const SchemaSettingsSortField = ()=>{
    const { fields } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
    const options = fields.filter((field)=>!(null == field ? void 0 : field.target) && 'sort' === field.interface).map((field)=>{
        var _field_uiSchema;
        return {
            value: null == field ? void 0 : field.name,
            label: compile(null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || (null == field ? void 0 : field.name)
        };
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaSettingsSelectItem, {
        title: t('Drag and drop sorting field'),
        options: options,
        value: field.decoratorProps.dragSortBy,
        onChange: (dragSortBy)=>{
            var _service_params;
            fieldSchema['x-decorator-props'].dragSortBy = dragSortBy;
            service.run({
                ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                sort: dragSortBy
            });
            field.decoratorProps.dragSortBy = dragSortBy;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-decorator-props': fieldSchema['x-decorator-props']
                }
            });
            dn.refresh();
        }
    }, "sort-field");
};
const isSystemField = (collectionField, getInterface)=>{
    const i = null == getInterface ? void 0 : getInterface(null == collectionField ? void 0 : collectionField.interface);
    return (null == i ? void 0 : i.group) === 'systemInfo';
};
function getFieldDefaultValue(fieldSchema, collectionField) {
    var _fieldSchema_default;
    const result = null !== (_fieldSchema_default = null == fieldSchema ? void 0 : fieldSchema.default) && void 0 !== _fieldSchema_default ? _fieldSchema_default : null == collectionField ? void 0 : collectionField.defaultValue;
    return result;
}
export { SchemaSettingsActionModalItem, SchemaSettingsBlockTitleItem, SchemaSettingsCascaderItem, SchemaSettingsConnectDataBlocks, SchemaSettingsDataTemplates, SchemaSettingsDefaultSortingRules, SchemaSettingsDivider, SchemaSettingsDropdown, SchemaSettingsEnableChildCollections, SchemaSettingsFormItemTemplate, SchemaSettingsItem, SchemaSettingsItemGroup, SchemaSettingsLinkageRules, SchemaSettingsModalItem, SchemaSettingsPopupItem, SchemaSettingsProvider, SchemaSettingsRemove, SchemaSettingsSelectItem, SchemaSettingsSortField, SchemaSettingsSubMenu, SchemaSettingsSwitchItem, SchemaSettingsTemplate, findParentFieldSchema, getFieldDefaultValue, isSystemField, useDataTemplates, useSchemaSettings };
