import { jsx } from "react/jsx-runtime";
import { useCallback, useMemo } from "react";
import { FormOutlined } from "@ant-design/icons";
import { SchemaInitializerItem, useSchemaInitializer, useSchemaInitializerItem } from "../../application/index.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { createCreateFormBlockUISchema } from "../../modules/blocks/data-blocks/form/createCreateFormBlockUISchema.mjs";
import { useSchemaTemplateManager } from "../../schema-templates/index.mjs";
import { useRecordCollectionDataSourceItems } from "../utils.mjs";
const RecordAssociationFormBlockInitializer = ()=>{
    const itemConfig = useSchemaInitializerItem();
    const { onCreateBlockSchema, componentType, createBlockSchema, ...others } = itemConfig;
    const { insert } = useSchemaInitializer();
    const { getCollection } = useCollectionManager_deprecated();
    const { getTemplateSchemaByMode } = useSchemaTemplateManager();
    const field = itemConfig.field;
    const collectionName = field.target;
    const collection = useMemo(()=>getCollection(collectionName), [
        collectionName
    ]);
    return /*#__PURE__*/ jsx(SchemaInitializerItem, {
        icon: /*#__PURE__*/ jsx(FormOutlined, {}),
        ...others,
        onClick: async (param)=>{
            let { item } = param;
            if (item.template) {
                const template = await getTemplateSchemaByMode(item);
                if ('FormItem' === item.template.componentName) {
                    const blockSchema = createCreateFormBlockUISchema({
                        dataSource: collection.dataSource,
                        association: "".concat(field.collectionName, ".").concat(field.name),
                        templateSchema: template
                    });
                    if ('reference' === item.mode) blockSchema['x-template-key'] = item.template.key;
                    insert(blockSchema);
                } else insert(template);
            } else insert(createCreateFormBlockUISchema({
                dataSource: collection.dataSource,
                association: "".concat(field.collectionName, ".").concat(field.name)
            }));
        },
        items: useRecordCollectionDataSourceItems('FormItem', itemConfig, collection, "".concat(field.collectionName, ".").concat(field.name))
    });
};
function useCreateAssociationFormBlock() {
    const { insert } = useSchemaInitializer();
    const { getCollection } = useCollectionManager_deprecated();
    const createAssociationFormBlock = useCallback((param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        insert(createCreateFormBlockUISchema({
            dataSource: collection.dataSource,
            association: "".concat(field.collectionName, ".").concat(field.name)
        }));
    }, [
        getCollection,
        insert
    ]);
    const templateWrap = useCallback((templateSchema, param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        if ('FormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = createCreateFormBlockUISchema({
                dataSource: collection.dataSource,
                association: "".concat(field.collectionName, ".").concat(field.name),
                templateSchema: templateSchema
            });
            if ('reference' === item.mode) blockSchema['x-template-key'] = item.template.key;
            return blockSchema;
        }
    }, [
        getCollection
    ]);
    return {
        createAssociationFormBlock,
        templateWrap
    };
}
export { RecordAssociationFormBlockInitializer, useCreateAssociationFormBlock };
