import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_AssociationProvider_mjs__ from "../../../../data-source/collection/AssociationProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_initializer_items_DataBlockInitializer_mjs__ from "../../../../schema-initializer/items/DataBlockInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__createCreateFormBlockUISchema_mjs__ from "./createCreateFormBlockUISchema.mjs";
const FormBlockInitializer = (param)=>{
    let { filterCollections, onlyCurrentDataSource, hideSearch, createBlockSchema, componentType = 'FormItem', templateWrap: customizeTemplateWrap, showAssociationFields, hideChildrenIfSingleCollection, hideOtherRecordsInPopup, currentText, otherText } = param;
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const { createFormBlock, templateWrap } = useCreateFormBlock();
    const onCreateFormBlockSchema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((options)=>{
        if (createBlockSchema) return createBlockSchema(options);
        createFormBlock(options);
    }, [
        createBlockSchema,
        createFormBlock
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_initializer_items_DataBlockInitializer_mjs__.DataBlockInitializer, {
        ...itemConfig,
        icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.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 } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const association = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_AssociationProvider_mjs__.useAssociationName)();
    const { isCusomeizeCreate: isCustomizeCreate } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const createFormBlock = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { item, fromOthersInPopup } = param;
        fromOthersInPopup ? insert((0, __WEBPACK_EXTERNAL_MODULE__createCreateFormBlockUISchema_mjs__.createCreateFormBlockUISchema)({
            collectionName: item.collectionName || item.name,
            dataSource: item.dataSource,
            isCusomeizeCreate: true
        })) : insert((0, __WEBPACK_EXTERNAL_MODULE__createCreateFormBlockUISchema_mjs__.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 = (0, __WEBPACK_EXTERNAL_MODULE__createCreateFormBlockUISchema_mjs__.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 };
