import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
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__schema_component_index_mjs__ from "../../schema-component/index.mjs";
const EnableChildCollections = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { defaultValues, collectionName } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useProps)(props);
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { getChildrenCollections } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const childrenCollections = getChildrenCollections(collectionName);
    const useAsyncDataSource = (service)=>(field, options)=>{
            field.loading = true;
            service(field, options).then(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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'
});
export { EnableChildCollections };
