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__collection_manager_index_mjs__ from "../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_form_createCreateFormBlockUISchema_mjs__ from "../../modules/blocks/data-blocks/form/createCreateFormBlockUISchema.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__ from "../../schema-templates/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
const RecordAssociationFormBlockInitializer = ()=>{
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const { onCreateBlockSchema, componentType, createBlockSchema, ...others } = itemConfig;
    const { insert } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { getTemplateSchemaByMode } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const field = itemConfig.field;
    const collectionName = field.target;
    const collection = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getCollection(collectionName), [
        collectionName
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerItem, {
        icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.FormOutlined, {}),
        ...others,
        onClick: async (param)=>{
            let { item } = param;
            if (item.template) {
                const template = await getTemplateSchemaByMode(item);
                if ('FormItem' === item.template.componentName) {
                    const blockSchema = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_form_createCreateFormBlockUISchema_mjs__.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((0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_form_createCreateFormBlockUISchema_mjs__.createCreateFormBlockUISchema)({
                dataSource: collection.dataSource,
                association: "".concat(field.collectionName, ".").concat(field.name)
            }));
        },
        items: (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useRecordCollectionDataSourceItems)('FormItem', itemConfig, collection, "".concat(field.collectionName, ".").concat(field.name))
    });
};
function useCreateAssociationFormBlock() {
    const { insert } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const createAssociationFormBlock = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        insert((0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_form_createCreateFormBlockUISchema_mjs__.createCreateFormBlockUISchema)({
            dataSource: collection.dataSource,
            association: "".concat(field.collectionName, ".").concat(field.name)
        }));
    }, [
        getCollection,
        insert
    ]);
    const templateWrap = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((templateSchema, param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        if ('FormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_form_createCreateFormBlockUISchema_mjs__.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 };
