import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { FormOutlined } from "@ant-design/icons";
import { SchemaInitializerItem, useSchemaInitializer, useSchemaInitializerItem } from "../../application/index.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { createDetailsUISchema } from "../../modules/blocks/data-blocks/details-single/createDetailsUISchema.mjs";
import { useSchemaTemplateManager } from "../../schema-templates/index.mjs";
import { useRecordCollectionDataSourceItems } from "../utils.mjs";
const RecordReadPrettyAssociationFormBlockInitializer = ()=>{
    const itemConfig = useSchemaInitializerItem();
    const { onCreateBlockSchema, componentType, createBlockSchema, ...others } = itemConfig;
    const { insert } = useSchemaInitializer();
    const { getCollection } = useCollectionManager_deprecated();
    const { getTemplateSchemaByMode } = useSchemaTemplateManager();
    const field = itemConfig.field;
    const collectionName = field.target;
    const collection = getCollection(collectionName);
    const resource = "".concat(field.collectionName, ".").concat(field.name);
    return /*#__PURE__*/ jsx(SchemaInitializerItem, {
        icon: /*#__PURE__*/ jsx(FormOutlined, {}),
        ...others,
        onClick: async (param)=>{
            let { item } = param;
            if (item.template) {
                const s = await getTemplateSchemaByMode(item);
                if ('ReadPrettyFormItem' === item.template.componentName) {
                    const blockSchema = createDetailsUISchema({
                        dataSource: collection.dataSource,
                        association: resource,
                        templateSchema: s
                    });
                    if ('reference' === item.mode) blockSchema['x-template-key'] = item.template.key;
                    insert(blockSchema);
                } else insert(s);
            } else insert(createDetailsUISchema({
                association: resource,
                dataSource: collection.dataSource
            }));
        },
        items: useRecordCollectionDataSourceItems('ReadPrettyFormItem', itemConfig, collection, resource)
    });
};
function useCreateAssociationDetailsWithoutPagination() {
    const { insert } = useSchemaInitializer();
    const { getCollection } = useCollectionManager_deprecated();
    const createAssociationDetailsWithoutPagination = useCallback((param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        insert(createDetailsUISchema({
            dataSource: collection.dataSource,
            association: "".concat(field.collectionName, ".").concat(field.name)
        }));
    }, [
        getCollection,
        insert
    ]);
    const templateWrap = useCallback((templateSchema, param)=>{
        let { item } = param;
        const field = item.associationField;
        const collection = getCollection(field.target);
        if ('ReadPrettyFormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = createDetailsUISchema({
                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 {
        createAssociationDetailsWithoutPagination,
        templateWrap
    };
}
export { RecordReadPrettyAssociationFormBlockInitializer, useCreateAssociationDetailsWithoutPagination };
