import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_schema_initializer_SchemaInitializer_mjs__ from "../../application/schema-initializer/SchemaInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_multi_DetailsBlockInitializer_mjs__ from "../../modules/blocks/data-blocks/details-multi/DetailsBlockInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_single_RecordReadPrettyFormBlockInitializer_mjs__ from "../../modules/blocks/data-blocks/details-single/RecordReadPrettyFormBlockInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_grid_card_GridCardBlockInitializer_mjs__ from "../../modules/blocks/data-blocks/grid-card/GridCardBlockInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_list_ListBlockInitializer_mjs__ from "../../modules/blocks/data-blocks/list/ListBlockInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
const canMakeAssociationBlock = (field)=>[
        'linkTo',
        'subTable',
        'o2m',
        'm2m',
        'obo',
        'oho',
        'o2o',
        'm2o'
    ].includes(field.interface);
function useRecordBlocks() {
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
    const { getChildrenCollections } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    const collectionsWithView = getChildrenCollections(collection.name, true, collection.dataSource).filter((v)=>null == v ? void 0 : v.filterTargetKey);
    const res = [
        {
            name: 'details',
            title: '{{t("Details")}}',
            Component: 'DetailsBlockInitializer',
            collectionName: collection.name,
            dataSource: collection.dataSource,
            useComponentProps () {
                const currentCollection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
                const { createSingleDetailsSchema, templateWrap } = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_single_RecordReadPrettyFormBlockInitializer_mjs__.useCreateSingleDetailsSchema)();
                const { createAssociationDetailsBlock } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationDetailsBlock)();
                const { createAssociationDetailsWithoutPagination, templateWrap: templateWrapOfAssociationDetailsWithoutPagination } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationDetailsWithoutPagination)();
                const { createDetailsBlock } = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_multi_DetailsBlockInitializer_mjs__.useCreateDetailsBlock)();
                const collectionsNeedToDisplay = [
                    currentCollection,
                    ...collectionsWithView
                ];
                const createBlockSchema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
                    let { item, fromOthersInPopup } = param;
                    if (fromOthersInPopup) return createDetailsBlock({
                        item
                    });
                    if (item.associationField) {
                        if ([
                            'hasOne',
                            'belongsTo'
                        ].includes(item.associationField.type)) return createAssociationDetailsWithoutPagination({
                            item
                        });
                        return createAssociationDetailsBlock({
                            item
                        });
                    }
                    return createSingleDetailsSchema({
                        item
                    });
                }, [
                    createAssociationDetailsBlock,
                    createAssociationDetailsWithoutPagination,
                    createDetailsBlock,
                    createSingleDetailsSchema
                ]);
                return {
                    filterCollections (param) {
                        let { collection, associationField } = param;
                        if (collection) return collectionsNeedToDisplay.some((c)=>c.name === collection.name);
                        if (associationField) return true;
                        return false;
                    },
                    onlyCurrentDataSource: true,
                    hideSearch: true,
                    componentType: 'ReadPrettyFormItem',
                    createBlockSchema,
                    templateWrap: (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((templateSchema, param)=>{
                        let { item } = param;
                        if (item.associationField) {
                            if ([
                                'hasOne',
                                'belongsTo'
                            ].includes(item.associationField.type)) return templateWrapOfAssociationDetailsWithoutPagination(templateSchema, {
                                item
                            });
                            return templateSchema;
                        }
                        return templateWrap(templateSchema, {
                            item
                        });
                    }, [
                        templateWrap,
                        templateWrapOfAssociationDetailsWithoutPagination
                    ]),
                    showAssociationFields: true
                };
            }
        },
        {
            name: 'editForm',
            title: '{{t("Form (Edit)")}}',
            Component: 'FormBlockInitializer',
            collectionName: collection.name,
            dataSource: collection.dataSource,
            useComponentProps () {
                const currentCollection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
                const { createEditFormBlock, templateWrap: templateWrapEdit } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateEditFormBlock)();
                const collectionsNeedToDisplay = [
                    currentCollection,
                    ...collectionsWithView
                ];
                return {
                    filterCollections (param) {
                        let { collection } = param;
                        if (collection) return collectionsNeedToDisplay.some((c)=>c.name === collection.name);
                        return true;
                    },
                    onlyCurrentDataSource: true,
                    hideSearch: true,
                    componentType: 'FormItem',
                    createBlockSchema: createEditFormBlock,
                    templateWrap: templateWrapEdit,
                    hideOtherRecordsInPopup: true,
                    showAssociationFields: true
                };
            },
            useVisible () {
                return ('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template;
            }
        },
        {
            name: 'createForm',
            title: '{{t("Form (Add new)")}}',
            Component: 'FormBlockInitializer',
            collectionName: collection.name,
            dataSource: collection.dataSource,
            useComponentProps () {
                const { createAssociationFormBlock, templateWrap } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationFormBlock)();
                const { createFormBlock, templateWrap: templateWrapCollection } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateFormBlock)();
                return {
                    filterCollections (param) {
                        let { collection, associationField } = param;
                        if (associationField) return [
                            'hasMany',
                            'belongsToMany'
                        ].includes(associationField.type);
                        return false;
                    },
                    onlyCurrentDataSource: true,
                    hideSearch: true,
                    componentType: 'FormItem',
                    createBlockSchema: (param)=>{
                        let { item, fromOthersInPopup } = param;
                        if (fromOthersInPopup) return createFormBlock({
                            item,
                            fromOthersInPopup
                        });
                        createAssociationFormBlock({
                            item
                        });
                    },
                    templateWrap: (templateSchema, param)=>{
                        let { item, fromOthersInPopup } = param;
                        if (fromOthersInPopup) return templateWrapCollection(templateSchema, {
                            item
                        });
                        templateWrap(templateSchema, {
                            item
                        });
                    },
                    showAssociationFields: true
                };
            }
        },
        {
            name: 'table',
            title: '{{t("Table")}}',
            Component: 'TableBlockInitializer',
            useComponentProps () {
                const { createAssociationTableBlock } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationTableBlock)();
                const { createTableBlock } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateTableBlock)();
                return {
                    hideSearch: true,
                    onlyCurrentDataSource: true,
                    filterCollections (param) {
                        let { associationField } = param;
                        if (associationField) return [
                            'hasMany',
                            'belongsToMany'
                        ].includes(associationField.type);
                        return false;
                    },
                    createBlockSchema: (param)=>{
                        let { item, fromOthersInPopup } = param;
                        if (fromOthersInPopup) return createTableBlock({
                            item
                        });
                        createAssociationTableBlock({
                            item
                        });
                    },
                    showAssociationFields: true
                };
            }
        },
        {
            name: 'list',
            title: '{{t("List")}}',
            Component: 'ListBlockInitializer',
            useComponentProps () {
                const { createAssociationListBlock } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationListBlock)();
                const { createListBlock } = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_list_ListBlockInitializer_mjs__.useCreateListBlock)();
                return {
                    hideSearch: true,
                    onlyCurrentDataSource: true,
                    filterCollections (param) {
                        let { associationField } = param;
                        if (associationField) return [
                            'hasMany',
                            'belongsToMany'
                        ].includes(associationField.type);
                        return false;
                    },
                    createBlockSchema: (param)=>{
                        let { item, fromOthersInPopup } = param;
                        if (fromOthersInPopup) return createListBlock({
                            item
                        });
                        createAssociationListBlock({
                            item
                        });
                    },
                    showAssociationFields: true
                };
            }
        },
        {
            name: 'gridCard',
            title: '{{t("Grid Card")}}',
            Component: 'GridCardBlockInitializer',
            useComponentProps () {
                const { createAssociationGridCardBlock } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCreateAssociationGridCardBlock)();
                const { createGridCardBlock } = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_grid_card_GridCardBlockInitializer_mjs__.useCreateGridCardBlock)();
                return {
                    hideSearch: true,
                    onlyCurrentDataSource: true,
                    filterCollections (param) {
                        let { associationField } = param;
                        if (associationField) return [
                            'hasMany',
                            'belongsToMany'
                        ].includes(associationField.type);
                        return false;
                    },
                    createBlockSchema: (param)=>{
                        let { item, fromOthersInPopup } = param;
                        if (fromOthersInPopup) return createGridCardBlock({
                            item
                        });
                        createAssociationGridCardBlock({
                            item
                        });
                    },
                    showAssociationFields: true
                };
            }
        }
    ];
    return res;
}
const recordBlockInitializers = new __WEBPACK_EXTERNAL_MODULE__application_schema_initializer_SchemaInitializer_mjs__.SchemaInitializer({
    name: 'popup:common:addBlock',
    wrap: __WEBPACK_EXTERNAL_MODULE__utils_mjs__.gridRowColWrap,
    title: '{{t("Add block")}}',
    icon: 'PlusOutlined',
    items: [
        {
            type: 'itemGroup',
            name: 'dataBlocks',
            title: '{{t("Data blocks")}}',
            useChildren: useRecordBlocks
        },
        {
            name: 'filterBlocks',
            title: '{{t("Filter blocks")}}',
            type: 'itemGroup',
            useVisible () {
                const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
                return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collection.fields.some((field)=>canMakeAssociationBlock(field) && [
                            'hasMany',
                            'belongsToMany'
                        ].includes(field.type)), [
                    collection.fields
                ]);
            },
            children: [
                {
                    name: 'filterForm',
                    title: '{{t("Form")}}',
                    Component: 'FilterFormBlockInitializer',
                    useComponentProps () {
                        const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
                        const toManyField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collection.fields.filter((field)=>[
                                    'hasMany',
                                    'belongsToMany'
                                ].includes(field.type)), [
                            collection.fields
                        ]);
                        return {
                            filterCollections (param) {
                                let { collection } = param;
                                if (collection) return toManyField.some((field)=>field.target === collection.name);
                            },
                            onlyCurrentDataSource: true
                        };
                    }
                },
                {
                    name: 'filterCollapse',
                    title: '{{t("Collapse")}}',
                    Component: 'FilterCollapseBlockInitializer',
                    useComponentProps () {
                        const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
                        const toManyField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collection.fields.filter((field)=>[
                                    'hasMany',
                                    'belongsToMany'
                                ].includes(field.type)), [
                            collection.fields
                        ]);
                        return {
                            filterCollections (param) {
                                let { collection } = param;
                                if (collection) return toManyField.some((field)=>field.target === collection.name);
                            },
                            onlyCurrentDataSource: true
                        };
                    }
                },
                {
                    name: 'filterTree',
                    title: '{{t("Tree")}}',
                    Component: 'FilterTreeBlockInitializer',
                    useComponentProps () {
                        const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
                        const toManyField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>collection.fields.filter((field)=>[
                                    'hasMany',
                                    'belongsToMany'
                                ].includes(field.type)), [
                            collection.fields
                        ]);
                        return {
                            filterCollections (param) {
                                let { collection } = param;
                                if (collection) return toManyField.some((field)=>field.target === collection.name);
                            },
                            onlyCurrentDataSource: true
                        };
                    }
                }
            ]
        },
        {
            type: 'itemGroup',
            name: 'otherBlocks',
            title: '{{t("Other blocks")}}',
            children: [
                {
                    name: 'markdown',
                    title: '{{t("Demonstration text")}}',
                    Component: 'MarkdownBlockInitializer'
                }
            ]
        }
    ]
});
export { canMakeAssociationBlock, recordBlockInitializers };
