"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    FormDataTemplates: ()=>FormDataTemplates,
    compatibleDataId: ()=>compatibleDataId,
    getLabel: ()=>getLabel
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const withDynamicSchemaProps_js_namespaceObject = require("../../application/hoc/withDynamicSchemaProps.js");
const index_js_namespaceObject = require("../../collection-manager/index.js");
const utils_js_namespaceObject = require("../../filter-provider/utils.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_VariableInput_index_js_namespaceObject = require("../VariableInput/index.js");
const AsDefaultTemplate_js_namespaceObject = require("./components/AsDefaultTemplate.js");
const DataTemplateTitle_js_namespaceObject = require("./components/DataTemplateTitle.js");
const Designer_js_namespaceObject = require("./components/Designer.js");
const useCollectionState_js_namespaceObject = require("./hooks/useCollectionState.js");
const external_utils_js_namespaceObject = require("./utils.js");
const Tree = (0, schema_namespaceObject.connect)(external_antd_namespaceObject.Tree, (0, schema_namespaceObject.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, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((0, schema_namespaceObject.observer)((props)=>{
    const { formSchema, designerCtx, defaultValues, collectionName } = (0, external_schema_component_index_js_namespaceObject.useProps)(props);
    const { collectionList, getEnableFieldTree, getOnLoadData, getOnCheck, getScopeDataSource, useTitleFieldDataSource } = (0, useCollectionState_js_namespaceObject.useCollectionState)(collectionName);
    const { getCollection, getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const activeData = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.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 external_lodash_default().isEmpty(filter) ? {} : (0, external_schema_component_index_js_namespaceObject.removeNullCondition)((0, utils_js_namespaceObject.mergeFilter)([
            filter,
            (0, Designer_js_namespaceObject.getSelectedIdFilter)(value)
        ], '$or'));
    };
    const components = (0, external_react_namespaceObject.useMemo)(()=>({
            ArrayCollapse: DataTemplateTitle_js_namespaceObject.ArrayCollapse
        }), []);
    const scope = (0, external_react_namespaceObject.useMemo)(()=>({
            getEnableFieldTree,
            getTargetField,
            getFieldNames,
            getFilter,
            getResource,
            getOnLoadData,
            getOnCheck,
            collectionName,
            getScopeDataSource,
            useTitleFieldDataSource
        }), []);
    const schema = (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(AsDefaultTemplate_js_namespaceObject.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: external_VariableInput_index_js_namespaceObject.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, external_utils_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponentContext.Provider, {
        value: {
            ...designerCtx,
            designable: true
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.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;
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
