"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    EnableChildCollections: ()=>EnableChildCollections
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const withDynamicSchemaProps_js_namespaceObject = require("../../application/hoc/withDynamicSchemaProps.js");
const index_js_namespaceObject = require("../../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const EnableChildCollections = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((0, schema_namespaceObject.observer)((props)=>{
    const { defaultValues, collectionName } = (0, external_schema_component_index_js_namespaceObject.useProps)(props);
    const form = (0, schema_namespaceObject.useForm)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { getChildrenCollections } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const childrenCollections = getChildrenCollections(collectionName);
    const useAsyncDataSource = (service)=>(field, options)=>{
            field.loading = true;
            service(field, options).then(schema_namespaceObject.action.bound((data)=>{
                field.dataSource = data;
                field.loading = false;
                if (field.initialValue) field.disabled = true;
            }));
        };
    const loadData = async (field)=>{
        var _form_values, _childrenCollections_filter;
        const { childrenCollections: childCollections } = (null === (_form_values = form.values) || void 0 === _form_values ? void 0 : _form_values.enableChildren) || {};
        return null === (_childrenCollections_filter = childrenCollections.filter((v)=>!childCollections.find((k)=>k.collection === v.name) || field.initialValue || v.name === field.value)) || void 0 === _childrenCollections_filter ? void 0 : _childrenCollections_filter.map((collection)=>({
                label: compile(collection.title),
                value: collection.name
            }));
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
        schema: {
            type: 'object',
            properties: {
                childrenCollections: {
                    type: 'array',
                    default: null == defaultValues ? void 0 : defaultValues.filter((v)=>childrenCollections.find((k)=>k.name === v.collection)),
                    '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'
                                    },
                                    collection: {
                                        type: 'string',
                                        'x-decorator': 'FormItem',
                                        required: true,
                                        'x-component': 'Select',
                                        'x-component-props': {
                                            style: {
                                                width: 260
                                            }
                                        },
                                        'x-reactions': [
                                            '{{useAsyncDataSource(loadData)}}'
                                        ]
                                    },
                                    title: {
                                        type: 'string',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Input',
                                        'x-component-props': {
                                            style: {
                                                width: 235
                                            }
                                        }
                                    },
                                    remove: {
                                        type: 'void',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'ArrayItems.Remove'
                                    }
                                }
                            }
                        }
                    },
                    properties: {
                        add: {
                            type: 'void',
                            title: '{{ t("Add collection") }}',
                            'x-component': 'ArrayItems.Addition'
                        }
                    }
                }
            }
        },
        scope: {
            useAsyncDataSource,
            loadData
        }
    });
}), {
    displayName: 'EnableChildCollections'
});
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
});
