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__createDetailsUISchema_mjs__ from "./createDetailsUISchema.mjs";
const RecordReadPrettyFormBlockInitializer = ()=>{
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const { icon = true, targetCollection, ...others } = itemConfig;
    const currentCollection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const collection = targetCollection || currentCollection;
    const { createSingleDetailsSchema } = useCreateSingleDetailsSchema();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerItem, {
        icon: icon && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.FormOutlined, {}),
        ...others,
        onClick: (options)=>createSingleDetailsSchema(options),
        items: (0, __WEBPACK_EXTERNAL_MODULE__schema_initializer_utils_mjs__.useRecordCollectionDataSourceItems)('ReadPrettyFormItem', null, null == collection ? void 0 : collection.name)
    });
};
function useCreateSingleDetailsSchema() {
    const { insert } = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializer)();
    const { getTemplateSchemaByMode } = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplateManager)();
    const association = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useAssociationName)();
    const templateWrap = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((templateSchema, options)=>{
        const { item } = options;
        if ('ReadPrettyFormItem' !== item.template.componentName) return templateSchema;
        {
            const blockSchema = (0, __WEBPACK_EXTERNAL_MODULE__createDetailsUISchema_mjs__.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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async (param)=>{
        let { item } = param;
        if (item.template) {
            const template = await getTemplateSchemaByMode(item);
            insert(templateWrap(template, {
                item
            }));
        } else insert((0, __WEBPACK_EXTERNAL_MODULE__createDetailsUISchema_mjs__.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 };
