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__data_source_index_mjs__ from "../../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__ from "../../../../schema-initializer/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__ from "../../../../schema-templates/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__createEditFormBlockUISchema_mjs__ from "./createEditFormBlockUISchema.mjs";
const RecordFormBlockInitializer = ()=>{
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const { targetCollection, ...others } = itemConfig;
    const { insert } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const { getTemplateSchemaByMode } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const currentCollection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const collection = targetCollection || currentCollection;
    const { createEditFormBlock, templateWrap } = useCreateEditFormBlock();
    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);
                insert(templateWrap(template, {
                    item
                }));
            } else createEditFormBlock({
                item
            });
        },
        items: (0, __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__.useRecordCollectionDataSourceItems)('FormItem', null, null == collection ? void 0 : collection.name)
    });
};
function useCreateEditFormBlock() {
    const { insert } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const association = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useAssociationName)();
    const createEditFormBlock = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { item } = param;
        const collectionName = item.collectionName || item.name;
        if (!association && item.associationField) {
            const field = item.associationField;
            insert((0, __WEBPACK_EXTERNAL_MODULE__createEditFormBlockUISchema_mjs__.createEditFormBlockUISchema)({
                dataSource: item.dataSource,
                association: "".concat(field.collectionName, ".").concat(field.name)
            }));
        } else insert((0, __WEBPACK_EXTERNAL_MODULE__createEditFormBlockUISchema_mjs__.createEditFormBlockUISchema)(association ? {
            association,
            dataSource: item.dataSource,
            isCurrent: true
        } : {
            collectionName,
            dataSource: item.dataSource
        }));
    }, [
        association,
        insert
    ]);
    const templateWrap = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((templateSchema, param)=>{
        let { item } = param;
        if ('FormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = (0, __WEBPACK_EXTERNAL_MODULE__createEditFormBlockUISchema_mjs__.createEditFormBlockUISchema)(association ? {
                association,
                dataSource: item.dataSource,
                templateSchema: templateSchema,
                isCurrent: true
            } : {
                collectionName: item.collectionName || item.name,
                dataSource: item.dataSource,
                templateSchema: templateSchema
            });
            if ('reference' === item.mode) blockSchema['x-template-key'] = item.template.key;
            return blockSchema;
        }
    }, [
        association
    ]);
    return {
        createEditFormBlock,
        templateWrap
    };
}
export { RecordFormBlockInitializer, useCreateEditFormBlock };
