import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { useFieldSchema } from "@tachybase/schema";
import { FormOutlined } from "@ant-design/icons";
import { SchemaInitializerItem, useSchemaInitializer, useSchemaInitializerItem } from "../../../../application/index.mjs";
import { useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { useAssociationName, useCollectionManager } from "../../../../data-source/index.mjs";
import { useRecordCollectionDataSourceItems } from "../../../../schema-initializer/utils.mjs";
import { useSchemaTemplateManager } from "../../../../schema-templates/index.mjs";
import { createEditFormBlockUISchema } from "./createEditFormBlockUISchema.mjs";
const RecordFormBlockInitializer = ()=>{
    const itemConfig = useSchemaInitializerItem();
    const { targetCollection, ...others } = itemConfig;
    const { insert } = useSchemaInitializer();
    const { getTemplateSchemaByMode } = useSchemaTemplateManager();
    const currentCollection = useCollection_deprecated();
    const collection = targetCollection || currentCollection;
    const { createEditFormBlock, templateWrap } = useCreateEditFormBlock();
    return /*#__PURE__*/ jsx(SchemaInitializerItem, {
        icon: /*#__PURE__*/ jsx(FormOutlined, {}),
        ...others,
        onClick: async (param)=>{
            let { item } = param;
            if (item.template) {
                const template = await getTemplateSchemaByMode(item);
                insert(templateWrap(template, {
                    item
                }));
            } else createEditFormBlock({
                item
            });
        },
        items: useRecordCollectionDataSourceItems('FormItem', null, null == collection ? void 0 : collection.name)
    });
};
function useCreateEditFormBlock() {
    const { insert } = useSchemaInitializer();
    const cm = useCollectionManager();
    const fieldSchema = useFieldSchema();
    const actionType = null == fieldSchema ? void 0 : fieldSchema['actionType'];
    const association = useAssociationName();
    const createEditFormBlock = useCallback((param)=>{
        let { item } = param;
        const field = item.associationField;
        const collectionName = (null == field ? void 0 : field.target) ? cm.getCollection(field.target).name : item.collectionName || item.name;
        if (item.associationField) {
            const field = item.associationField;
            insert(createEditFormBlockUISchema({
                dataSource: item.dataSource,
                association: "".concat(field.collectionName, ".").concat(field.name)
            }));
        } else insert(createEditFormBlockUISchema(association && !actionType ? {
            association,
            dataSource: item.dataSource,
            isCurrent: true
        } : {
            dataSource: item.dataSource,
            collectionName
        }));
    }, [
        association,
        insert
    ]);
    const templateWrap = useCallback((templateSchema, param)=>{
        let { item } = param;
        if ('FormItem' !== item.template.componentName) return templateSchema;
        {
            const options = {
                dataSource: item.dataSource,
                templateSchema
            };
            if (item.associationField) {
                const field = item.associationField;
                options.association = "".concat(field.collectionName, ".").concat(field.name);
            } else if (association) {
                options.association = association;
                options.isCurrent = true;
            } else options.collectionName = item.collectionName || item.name;
            const blockSchema = createEditFormBlockUISchema(options);
            if ('reference' === item.mode) blockSchema['x-template-key'] = item.template.key;
            return blockSchema;
        }
    }, [
        association
    ]);
    return {
        createEditFormBlock,
        templateWrap
    };
}
export { RecordFormBlockInitializer, useCreateEditFormBlock };
