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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__ from "../../filter-provider/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__ from "../VariableInput/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_AsDefaultTemplate_mjs__ from "./components/AsDefaultTemplate.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_DataTemplateTitle_mjs__ from "./components/DataTemplateTitle.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_Designer_mjs__ from "./components/Designer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useCollectionState_mjs__ from "./hooks/useCollectionState.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "./utils.mjs";
const Tree = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)(__WEBPACK_EXTERNAL_MODULE_antd__.Tree, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    value: 'checkedKeys',
    dataSource: 'treeData'
}));
Tree.displayName = 'Tree';
const compatibleDataId = (data, config)=>null == data ? void 0 : data.map((v)=>{
        const { dataId, ...others } = v;
        const obj = {
            ...others
        };
        if (dataId) {
            var _config_v_collection;
            obj.dataScope = {
                $and: [
                    {
                        id: {
                            $eq: dataId
                        }
                    }
                ]
            };
            obj.titleField = (null == obj ? void 0 : obj.titleField) || (null == config ? void 0 : null === (_config_v_collection = config[v.collection]) || void 0 === _config_v_collection ? void 0 : _config_v_collection['titleField']) || 'id';
        }
        return obj;
    });
const FormDataTemplates = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { formSchema, designerCtx, defaultValues, collectionName } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useProps)(props);
    const { collectionList, getEnableFieldTree, getOnLoadData, getOnCheck, getScopeDataSource, useTitleFieldDataSource } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useCollectionState_mjs__.useCollectionState)(collectionName);
    const { getCollection, getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const activeData = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observable)({
            ...defaultValues,
            items: compatibleDataId((null == defaultValues ? void 0 : defaultValues.items) || [], null == defaultValues ? void 0 : defaultValues.config)
        }), []);
    const getTargetField = (collectionName)=>{
        var _activeData_config_collectionName;
        const collection = getCollection(collectionName);
        return getCollectionField("".concat(collectionName, ".").concat((null == activeData ? void 0 : null === (_activeData_config_collectionName = activeData.config[collectionName]) || void 0 === _activeData_config_collectionName ? void 0 : _activeData_config_collectionName.titleField) || (null == collection ? void 0 : collection.titleField) || 'id'));
    };
    const getFieldNames = (collectionName)=>{
        var _activeData_config_collectionName, _activeData_config;
        const collection = getCollection(collectionName);
        return {
            label: getLabel((null === (_activeData_config = activeData.config) || void 0 === _activeData_config ? void 0 : null === (_activeData_config_collectionName = _activeData_config[collectionName]) || void 0 === _activeData_config_collectionName ? void 0 : _activeData_config_collectionName.titleField) || (null == collection ? void 0 : collection.titleField) || 'id'),
            value: 'id'
        };
    };
    const getFilter = (collectionName, value)=>{
        var _activeData_config_collectionName, _activeData_config;
        const filter = null === (_activeData_config = activeData.config) || void 0 === _activeData_config ? void 0 : null === (_activeData_config_collectionName = _activeData_config[collectionName]) || void 0 === _activeData_config_collectionName ? void 0 : _activeData_config_collectionName.filter;
        return __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(filter) ? {} : (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)((0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
            filter,
            (0, __WEBPACK_EXTERNAL_MODULE__components_Designer_mjs__.getSelectedIdFilter)(value)
        ], '$or'));
    };
    const components = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__components_DataTemplateTitle_mjs__.ArrayCollapse
        }), []);
    const scope = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            getEnableFieldTree,
            getTargetField,
            getFieldNames,
            getFilter,
            getResource,
            getOnLoadData,
            getOnCheck,
            collectionName,
            getScopeDataSource,
            useTitleFieldDataSource
        }), []);
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            type: 'object',
            properties: {
                items: {
                    type: 'array',
                    default: null == activeData ? void 0 : activeData.items,
                    'x-component': 'ArrayCollapse',
                    'x-decorator': 'FormItem',
                    'x-component-props': {
                        accordion: true
                    },
                    items: {
                        type: 'object',
                        'x-component': 'ArrayCollapse.CollapsePanel',
                        'x-component-props': {
                            extra: [
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__components_AsDefaultTemplate_mjs__.AsDefaultTemplate, {}, "0")
                            ]
                        },
                        properties: {
                            layout: {
                                type: 'void',
                                'x-component': 'FormLayout',
                                'x-component-props': {
                                    layout: 'vertical'
                                },
                                properties: {
                                    collection: {
                                        type: 'string',
                                        title: '{{ t("Collection") }}',
                                        required: true,
                                        description: t('If collection inherits, choose inherited collections as templates'),
                                        default: '{{ collectionName }}',
                                        'x-display': collectionList.length > 1 ? 'visible' : 'hidden',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Select',
                                        'x-component-props': {
                                            options: collectionList
                                        }
                                    },
                                    dataScope: {
                                        type: 'object',
                                        title: '{{ t("Assign  data scope for the template") }}',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Filter',
                                        'x-component-props': {
                                            dynamicComponent: __WEBPACK_EXTERNAL_MODULE__VariableInput_index_mjs__.VariableInput
                                        },
                                        'x-decorator-props': {
                                            style: {
                                                marginBottom: '0px'
                                            }
                                        },
                                        required: true,
                                        'x-reactions': [
                                            {
                                                dependencies: [
                                                    '.collection'
                                                ],
                                                fulfill: {
                                                    state: {
                                                        disabled: '{{ !$deps[0] }}'
                                                    },
                                                    schema: {
                                                        enum: '{{ getScopeDataSource($deps[0]) }}'
                                                    }
                                                }
                                            }
                                        ]
                                    },
                                    titleField: {
                                        type: 'string',
                                        'x-decorator': 'FormItem',
                                        title: '{{ t("Title field") }}',
                                        'x-component': 'Select',
                                        required: true,
                                        'x-reactions': '{{useTitleFieldDataSource}}'
                                    },
                                    syncFromForm: {
                                        type: 'void',
                                        title: '{{ t("Sync from form fields") }}',
                                        'x-component': 'Action.Link',
                                        'x-component-props': {
                                            type: 'primary',
                                            style: {
                                                float: 'right',
                                                position: 'relative',
                                                zIndex: 1200
                                            },
                                            useAction: ()=>(0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useSyncFromForm)(formSchema)
                                        }
                                    },
                                    fields: {
                                        type: 'array',
                                        title: '{{ t("Data fields") }}',
                                        required: true,
                                        description: t('Only the selected fields will be used as the initialization data for the form'),
                                        'x-decorator': 'FormItem',
                                        'x-component': Tree,
                                        'x-component-props': {
                                            treeData: [],
                                            checkable: true,
                                            checkStrictly: true,
                                            selectable: false,
                                            loadData: '{{ getOnLoadData($self) }}',
                                            onCheck: '{{ getOnCheck($self) }}',
                                            rootStyle: {
                                                padding: '8px 0',
                                                border: '1px solid #d9d9d9',
                                                borderRadius: '2px',
                                                maxHeight: '30vh',
                                                overflow: 'auto',
                                                margin: '2px 0'
                                            }
                                        },
                                        'x-reactions': [
                                            {
                                                dependencies: [
                                                    '.collection'
                                                ],
                                                fulfill: {
                                                    state: {
                                                        disabled: '{{ !$deps[0] }}',
                                                        componentProps: {
                                                            treeData: '{{ getEnableFieldTree($deps[0], $self) }}'
                                                        }
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            },
                            remove: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.Remove'
                            },
                            moveUp: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.MoveUp'
                            },
                            moveDown: {
                                type: 'void',
                                'x-component': 'ArrayCollapse.MoveDown'
                            }
                        }
                    },
                    properties: {
                        add: {
                            type: 'void',
                            title: '{{ t("Add template") }}',
                            'x-component': 'ArrayCollapse.Addition'
                        }
                    }
                },
                display: {
                    type: 'boolean',
                    'x-content': '{{ t("Display data template selector") }}',
                    default: (null == activeData ? void 0 : activeData.display) !== false,
                    'x-decorator': 'FormItem',
                    'x-component': 'Checkbox'
                }
            }
        }), []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponentContext.Provider, {
        value: {
            ...designerCtx,
            designable: true
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
            components: components,
            scope: scope,
            schema: schema
        })
    });
}, {
    displayName: 'FormDataTemplates'
}), {
    displayName: 'FormDataTemplates'
});
function getLabel(titleField) {
    return titleField || 'label';
}
function getResource(resource, field) {
    if (resource !== field.componentProps.service.resource) field.value = void 0;
    return resource;
}
export { FormDataTemplates, compatibleDataId, getLabel };
