import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { RecursionField, observer, useField, useFieldSchema } from "@tachybase/schema";
import { CollectionProvider_deprecated, useCollectionManager_deprecated } from "../../../../collection-manager/index.mjs";
import { CreateAction } from "../../../../schema-initializer/components/index.mjs";
import { ActionContextProvider, useActionContext } from "../../action/index.mjs";
import { useAssociationFieldContext, useInsertSchema } from "../hooks.mjs";
import schema from "../schema.mjs";
const CreateRecordAction = observer((props)=>{
    const field = useField();
    const fieldSchema = useFieldSchema();
    const ctx = useActionContext();
    const { getCollection } = useCollectionManager_deprecated();
    const insertAddNewer = useInsertSchema('AddNewer');
    const { options: collectionField } = useAssociationFieldContext();
    const [visibleAddNewer, setVisibleAddNewer] = useState(false);
    const targetCollection = getCollection(null == collectionField ? void 0 : collectionField.target);
    const [currentCollection, setCurrentCollection] = useState(null == targetCollection ? void 0 : targetCollection.name);
    const [currentDataSource, setCurrentDataSource] = useState(null == targetCollection ? void 0 : targetCollection.dataSource);
    const addbuttonClick = (collectionData)=>{
        insertAddNewer(schema.AddNewer);
        setVisibleAddNewer(true);
        setCurrentCollection(collectionData.name);
        setCurrentDataSource(collectionData.dataSource);
    };
    return /*#__PURE__*/ jsxs(CollectionProvider_deprecated, {
        name: null == collectionField ? void 0 : collectionField.target,
        children: [
            /*#__PURE__*/ jsx(CreateAction, {
                ...props,
                onClick: (arg)=>addbuttonClick(arg)
            }),
            /*#__PURE__*/ jsx(ActionContextProvider, {
                value: {
                    ...ctx,
                    visible: visibleAddNewer,
                    setVisible: setVisibleAddNewer
                },
                children: /*#__PURE__*/ jsx(CollectionProvider_deprecated, {
                    name: currentCollection,
                    dataSource: currentDataSource,
                    children: /*#__PURE__*/ jsx(RecursionField, {
                        onlyRenderProperties: true,
                        basePath: field.address,
                        schema: fieldSchema,
                        filterProperties: (s)=>'AssociationField.AddNewer' === s['x-component']
                    })
                })
            })
        ]
    });
}, {
    displayName: 'CreateRecordAction'
});
export { CreateRecordAction };
