import { jsx } from "react/jsx-runtime";
import "react";
import { action, observer, useForm } from "@tachybase/schema";
import { withDynamicSchemaProps } from "../../application/hoc/withDynamicSchemaProps.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { SchemaComponent, useCompile, useProps } from "../../schema-component/index.mjs";
const EnableChildCollections = withDynamicSchemaProps(observer((props)=>{
    const { defaultValues, collectionName } = useProps(props);
    const form = useForm();
    const compile = useCompile();
    const { getChildrenCollections } = useCollectionManager_deprecated();
    const childrenCollections = getChildrenCollections(collectionName);
    const useAsyncDataSource = (service)=>(field, options)=>{
            field.loading = true;
            service(field, options).then(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.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.map((collection)=>({
                label: compile(collection.title),
                value: collection.name
            }));
    };
    return /*#__PURE__*/ jsx(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 };
