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 { useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { useAssociationName } from "../../../../data-source/index.mjs";
import { useRecordCollectionDataSourceItems } from "../../../../schema-initializer/utils.mjs";
import { useSchemaTemplateManager } from "../../../../schema-templates/index.mjs";
import { createDetailsUISchema } from "./createDetailsUISchema.mjs";
const RecordReadPrettyFormBlockInitializer = ()=>{
    const itemConfig = useSchemaInitializerItem();
    const { icon = true, targetCollection, ...others } = itemConfig;
    const currentCollection = useCollection_deprecated();
    const collection = targetCollection || currentCollection;
    const { createSingleDetailsSchema } = useCreateSingleDetailsSchema();
    return /*#__PURE__*/ jsx(SchemaInitializerItem, {
        icon: icon && /*#__PURE__*/ jsx(FormOutlined, {}),
        ...others,
        onClick: (options)=>createSingleDetailsSchema(options),
        items: useRecordCollectionDataSourceItems('ReadPrettyFormItem', null, null == collection ? void 0 : collection.name)
    });
};
function useCreateSingleDetailsSchema() {
    const { insert } = useSchemaInitializer();
    const { getTemplateSchemaByMode } = useSchemaTemplateManager();
    const association = useAssociationName();
    const templateWrap = useCallback((templateSchema, options)=>{
        const { item } = options;
        if ('ReadPrettyFormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = createDetailsUISchema(association ? {
                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
    ]);
    const createSingleDetailsSchema = useCallback(async (param)=>{
        let { item } = param;
        if (item.template) {
            const template = await getTemplateSchemaByMode(item);
            insert(templateWrap(template, {
                item
            }));
        } else insert(createDetailsUISchema(association ? {
            association,
            dataSource: item.dataSource,
            isCurrent: true
        } : {
            collectionName: item.collectionName || item.name,
            dataSource: item.dataSource
        }));
    }, [
        association,
        getTemplateSchemaByMode,
        insert,
        templateWrap
    ]);
    return {
        createSingleDetailsSchema,
        templateWrap
    };
}
export { RecordReadPrettyFormBlockInitializer, useCreateSingleDetailsSchema };
