"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    canMakeAssociationBlock: ()=>canMakeAssociationBlock,
    recordBlockInitializers: ()=>recordBlockInitializers
});
const external_react_namespaceObject = require("react");
const external_index_js_namespaceObject = require("../../index.js");
const SchemaInitializer_js_namespaceObject = require("../../application/schema-initializer/SchemaInitializer.js");
const DetailsBlockInitializer_js_namespaceObject = require("../../modules/blocks/data-blocks/details-multi/DetailsBlockInitializer.js");
const RecordReadPrettyFormBlockInitializer_js_namespaceObject = require("../../modules/blocks/data-blocks/details-single/RecordReadPrettyFormBlockInitializer.js");
const GridCardBlockInitializer_js_namespaceObject = require("../../modules/blocks/data-blocks/grid-card/GridCardBlockInitializer.js");
const ListBlockInitializer_js_namespaceObject = require("../../modules/blocks/data-blocks/list/ListBlockInitializer.js");
const external_utils_js_namespaceObject = require("../utils.js");
const canMakeAssociationBlock = (field)=>[
        'linkTo',
        'subTable',
        'o2m',
        'm2m',
        'obo',
        'oho',
        'o2o',
        'm2o'
    ].includes(field.interface);
function useRecordBlocks() {
    const collection = (0, external_index_js_namespaceObject.useCollection)();
    const { getChildrenCollections } = (0, external_index_js_namespaceObject.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, external_index_js_namespaceObject.useCollection_deprecated)();
                const { createSingleDetailsSchema, templateWrap } = (0, RecordReadPrettyFormBlockInitializer_js_namespaceObject.useCreateSingleDetailsSchema)();
                const { createAssociationDetailsBlock } = (0, external_index_js_namespaceObject.useCreateAssociationDetailsBlock)();
                const { createAssociationDetailsWithoutPagination, templateWrap: templateWrapOfAssociationDetailsWithoutPagination } = (0, external_index_js_namespaceObject.useCreateAssociationDetailsWithoutPagination)();
                const { createDetailsBlock } = (0, DetailsBlockInitializer_js_namespaceObject.useCreateDetailsBlock)();
                const collectionsNeedToDisplay = [
                    currentCollection,
                    ...collectionsWithView
                ];
                const createBlockSchema = (0, external_react_namespaceObject.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, external_react_namespaceObject.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, external_index_js_namespaceObject.useCollection_deprecated)();
                const { createEditFormBlock, templateWrap: templateWrapEdit } = (0, external_index_js_namespaceObject.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, external_index_js_namespaceObject.useCreateAssociationFormBlock)();
                const { createFormBlock, templateWrap: templateWrapCollection } = (0, external_index_js_namespaceObject.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, external_index_js_namespaceObject.useCreateAssociationTableBlock)();
                const { createTableBlock } = (0, external_index_js_namespaceObject.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, external_index_js_namespaceObject.useCreateAssociationListBlock)();
                const { createListBlock } = (0, ListBlockInitializer_js_namespaceObject.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, external_index_js_namespaceObject.useCreateAssociationGridCardBlock)();
                const { createGridCardBlock } = (0, GridCardBlockInitializer_js_namespaceObject.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 SchemaInitializer_js_namespaceObject.SchemaInitializer({
    name: 'popup:common:addBlock',
    wrap: external_utils_js_namespaceObject.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, external_index_js_namespaceObject.useCollection)();
                return (0, external_react_namespaceObject.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, external_index_js_namespaceObject.useCollection)();
                        const toManyField = (0, external_react_namespaceObject.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, external_index_js_namespaceObject.useCollection)();
                        const toManyField = (0, external_react_namespaceObject.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, external_index_js_namespaceObject.useCollection)();
                        const toManyField = (0, external_react_namespaceObject.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'
                }
            ]
        }
    ]
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
