import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { FormOutlined } from "@ant-design/icons";
import { useSchemaInitializer, useSchemaInitializerItem } from "../../../../application/index.mjs";
import { useAssociationName } from "../../../../data-source/collection/AssociationProvider.mjs";
import { DataBlockInitializer } from "../../../../schema-initializer/items/DataBlockInitializer.mjs";
import { createCreateFormBlockUISchema } from "./createCreateFormBlockUISchema.mjs";
const FormBlockInitializer = (param)=>{
    let { filterCollections, onlyCurrentDataSource, hideSearch, createBlockSchema, componentType = 'FormItem', templateWrap: customizeTemplateWrap, showAssociationFields, hideChildrenIfSingleCollection, hideOtherRecordsInPopup, currentText, otherText } = param;
    const itemConfig = useSchemaInitializerItem();
    const { createFormBlock, templateWrap } = useCreateFormBlock();
    const onCreateFormBlockSchema = useCallback((options)=>{
        if (createBlockSchema) return createBlockSchema(options);
        createFormBlock(options);
    }, [
        createBlockSchema,
        createFormBlock
    ]);
    return /*#__PURE__*/ jsx(DataBlockInitializer, {
        ...itemConfig,
        icon: /*#__PURE__*/ jsx(FormOutlined, {}),
        componentType: componentType,
        templateWrap: (templateSchema, options)=>{
            if (customizeTemplateWrap) return customizeTemplateWrap(templateSchema, options);
            return templateWrap(templateSchema, options);
        },
        onCreateBlockSchema: onCreateFormBlockSchema,
        filter: filterCollections,
        onlyCurrentDataSource: onlyCurrentDataSource,
        hideSearch: hideSearch,
        showAssociationFields: showAssociationFields,
        hideChildrenIfSingleCollection: hideChildrenIfSingleCollection,
        hideOtherRecordsInPopup: hideOtherRecordsInPopup,
        currentText: currentText,
        otherText: otherText
    });
};
const useCreateFormBlock = ()=>{
    const { insert } = useSchemaInitializer();
    const association = useAssociationName();
    const { isCusomeizeCreate: isCustomizeCreate } = useSchemaInitializerItem();
    const createFormBlock = useCallback((param)=>{
        let { item, fromOthersInPopup } = param;
        fromOthersInPopup ? insert(createCreateFormBlockUISchema({
            collectionName: item.collectionName || item.name,
            dataSource: item.dataSource,
            isCusomeizeCreate: true
        })) : insert(createCreateFormBlockUISchema(association ? {
            association,
            dataSource: item.dataSource,
            isCusomeizeCreate: isCustomizeCreate
        } : {
            collectionName: item.collectionName || item.name,
            dataSource: item.dataSource,
            isCusomeizeCreate: isCustomizeCreate
        }));
    }, [
        association,
        insert,
        isCustomizeCreate
    ]);
    const templateWrap = (templateSchema, param)=>{
        let { item } = param;
        const schema = createCreateFormBlockUISchema({
            isCusomeizeCreate: isCustomizeCreate,
            dataSource: item.dataSource,
            templateSchema: templateSchema,
            collectionName: item.name,
            association
        });
        if (item.template && 'reference' === item.mode) schema['x-template-key'] = item.template.key;
        return schema;
    };
    return {
        createFormBlock,
        templateWrap
    };
};
export { FormBlockInitializer, useCreateFormBlock };
