"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__, {
    EditSortField: ()=>EditSortField,
    TableBlockDesigner: ()=>TableBlockDesigner
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../api-client/index.js");
const external_block_provider_index_js_namespaceObject = require("../../../block-provider/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const action_hooks_js_namespaceObject = require("../../../collection-manager/action-hooks.js");
const utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const setDataLoadingModeSettingsItem_js_namespaceObject = require("../../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.js");
const external_schema_settings_index_js_namespaceObject = require("../../../schema-settings/index.js");
const SchemaSettingsDataScope_js_namespaceObject = require("../../../schema-settings/SchemaSettingsDataScope.js");
const external_schema_templates_index_js_namespaceObject = require("../../../schema-templates/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_filter_index_js_namespaceObject = require("../filter/index.js");
const FixedBlockDesignerItem_js_namespaceObject = require("../page/FixedBlockDesignerItem.js");
const EditSortField = ()=>{
    const { fields } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { dn } = (0, external_hooks_index_js_namespaceObject.useDesignable)();
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const { service } = (0, external_block_provider_index_js_namespaceObject.useTableBlockContext)();
    const options = fields.filter((field)=>!(null == field ? void 0 : field.target) && 'sort' === field.interface).map((field)=>{
        var _field_uiSchema;
        return {
            value: null == field ? void 0 : field.name,
            label: compile(null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || (null == field ? void 0 : field.name)
        };
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsSelectItem, {
        title: t('Drag and drop sorting field'),
        options: options,
        value: field.decoratorProps.dragSortBy,
        onChange: (dragSortBy)=>{
            var _service_params;
            fieldSchema['x-decorator-props'].dragSortBy = dragSortBy;
            service.run({
                ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                sort: dragSortBy
            });
            field.decoratorProps.dragSortBy = dragSortBy;
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-decorator-props': fieldSchema['x-decorator-props']
                }
            });
            dn.refresh();
        }
    }, "sort-field");
};
const TableBlockDesigner = ()=>{
    var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1, _fieldSchema_xdecoratorprops2, _fieldSchema_xdecoratorprops3, _getCollection, _fieldSchema_xdecoratorprops_params1, _fieldSchema_xdecoratorprops4, _field_decoratorProps_params, _field_decoratorProps;
    const { name, title } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionField, getCollection } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { form } = (0, external_block_provider_index_js_namespaceObject.useFormBlockContext)();
    const sortFields = (0, action_hooks_js_namespaceObject.useSortFields)(name);
    const { service } = (0, external_block_provider_index_js_namespaceObject.useTableBlockContext)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { dn } = (0, external_hooks_index_js_namespaceObject.useDesignable)();
    const defaultSort = (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops ? void 0 : null === (_fieldSchema_xdecoratorprops_params = _fieldSchema_xdecoratorprops.params) || void 0 === _fieldSchema_xdecoratorprops_params ? void 0 : _fieldSchema_xdecoratorprops_params.sort) || [];
    const defaultResource = (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops1 ? void 0 : _fieldSchema_xdecoratorprops1.resource) || (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops2 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops2 ? void 0 : _fieldSchema_xdecoratorprops2.association);
    const supportTemplate = !(null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops3 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops3 ? void 0 : _fieldSchema_xdecoratorprops3.disableTemplate);
    const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>(null == item ? void 0 : item.startsWith('-')) ? {
            field: item.substring(1),
            direction: 'desc'
        } : {
            field: item,
            direction: 'asc'
        });
    const template = (0, external_schema_templates_index_js_namespaceObject.useSchemaTemplate)();
    const collection = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { dragSort, resource } = field.decoratorProps;
    const collectionField = resource && getCollectionField(resource);
    const treeCollection = (null == resource ? void 0 : resource.includes('.')) ? null === (_getCollection = getCollection(null == collectionField ? void 0 : collectionField.target)) || void 0 === _getCollection ? void 0 : _getCollection.tree : !!(null == collection ? void 0 : collection.tree);
    const onDataScopeSubmit = (0, external_react_namespaceObject.useCallback)((param)=>{
        let { filter } = param;
        var _service_params_, _service_params, _service_params1;
        filter = (0, external_filter_index_js_namespaceObject.removeNullCondition)(filter);
        const params = field.decoratorProps.params || {};
        params.filter = filter;
        field.decoratorProps.params = params;
        fieldSchema['x-decorator-props']['params'] = params;
        const filters = (null === (_service_params = service.params) || void 0 === _service_params ? void 0 : null === (_service_params_ = _service_params[1]) || void 0 === _service_params_ ? void 0 : _service_params_.filters) || {};
        service.run({
            ...null === (_service_params1 = service.params) || void 0 === _service_params1 ? void 0 : _service_params1[0],
            filter: (0, utils_js_namespaceObject.mergeFilter)([
                ...Object.values(filters),
                filter
            ]),
            page: 1
        }, {
            filters
        });
        dn.emit('patch', {
            schema: {
                ['x-uid']: fieldSchema['x-uid'],
                'x-decorator-props': fieldSchema['x-decorator-props']
            }
        });
    }, [
        dn,
        field.decoratorProps,
        fieldSchema,
        service
    ]);
    const api = (0, index_js_namespaceObject.useAPIClient)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_settings_index_js_namespaceObject.GeneralSchemaDesigner, {
        template: template,
        title: title || name,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsBlockTitleItem, {}),
            (null == collection ? void 0 : collection.tree) && (null == collectionField ? void 0 : collectionField.collectionName) === (null == collectionField ? void 0 : collectionField.target) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsSwitchItem, {
                title: t('Tree table'),
                defaultChecked: true,
                checked: !!treeCollection && false !== field.decoratorProps.treeTable,
                onChange: (flag)=>{
                    var _service_params;
                    field.decoratorProps.treeTable = flag;
                    fieldSchema['x-decorator-props'].treeTable = flag;
                    const params = {
                        ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                        tree: !!flag || null
                    };
                    dn.emit('patch', {
                        schema: fieldSchema
                    });
                    dn.refresh();
                    service.run(params);
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsSwitchItem, {
                title: t('Enable drag and drop sorting'),
                checked: field.decoratorProps.dragSort,
                onChange: async (dragSort)=>{
                    if (dragSort && collectionField) {
                        const { data } = await api.resource('collections.fields', collectionField.collectionName).update({
                            filterByTk: collectionField.name,
                            values: {
                                sortable: true
                            }
                        });
                    }
                    field.decoratorProps.dragSort = dragSort;
                    fieldSchema['x-decorator-props'].dragSort = dragSort;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-decorator-props': fieldSchema['x-decorator-props']
                        }
                    });
                }
            }),
            field.decoratorProps.dragSort && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EditSortField, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FixedBlockDesignerItem_js_namespaceObject.FixedBlockDesignerItem, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SchemaSettingsDataScope_js_namespaceObject.SchemaSettingsDataScope, {
                collectionName: name,
                defaultFilter: (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops4 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops4 ? void 0 : null === (_fieldSchema_xdecoratorprops_params1 = _fieldSchema_xdecoratorprops4.params) || void 0 === _fieldSchema_xdecoratorprops_params1 ? void 0 : _fieldSchema_xdecoratorprops_params1.filter) || {},
                form: form,
                onSubmit: onDataScopeSubmit
            }),
            !dragSort && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsModalItem, {
                title: t('Set default sorting rules'),
                components: {
                    ArrayItems: components_namespaceObject.ArrayItems
                },
                schema: {
                    type: 'object',
                    title: t('Set default sorting rules'),
                    properties: {
                        sort: {
                            type: 'array',
                            default: sort,
                            'x-component': 'ArrayItems',
                            'x-decorator': 'FormItem',
                            items: {
                                type: 'object',
                                properties: {
                                    space: {
                                        type: 'void',
                                        'x-component': 'Space',
                                        properties: {
                                            sort: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.SortHandle'
                                            },
                                            field: {
                                                type: 'string',
                                                enum: sortFields,
                                                required: true,
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Select',
                                                'x-component-props': {
                                                    style: {
                                                        width: 260
                                                    }
                                                }
                                            },
                                            direction: {
                                                type: 'string',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Radio.Group',
                                                'x-component-props': {
                                                    optionType: 'button'
                                                },
                                                enum: [
                                                    {
                                                        label: t('ASC'),
                                                        value: 'asc'
                                                    },
                                                    {
                                                        label: t('DESC'),
                                                        value: 'desc'
                                                    }
                                                ]
                                            },
                                            remove: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.Remove'
                                            }
                                        }
                                    }
                                }
                            },
                            properties: {
                                add: {
                                    type: 'void',
                                    title: t('Add sort field'),
                                    'x-component': 'ArrayItems.Addition'
                                }
                            }
                        }
                    }
                },
                onSubmit: (param)=>{
                    let { sort } = param;
                    var _service_params;
                    const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
                    const params = field.decoratorProps.params || {};
                    params.sort = sortArr;
                    field.decoratorProps.params = params;
                    fieldSchema['x-decorator-props']['params'] = params;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-decorator-props': fieldSchema['x-decorator-props']
                        }
                    });
                    service.run({
                        ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                        sort: sortArr
                    });
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(setDataLoadingModeSettingsItem_js_namespaceObject.SetDataLoadingMode, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsSelectItem, {
                title: t('Records per page'),
                value: (null === (_field_decoratorProps = field.decoratorProps) || void 0 === _field_decoratorProps ? void 0 : null === (_field_decoratorProps_params = _field_decoratorProps.params) || void 0 === _field_decoratorProps_params ? void 0 : _field_decoratorProps_params.pageSize) || 20,
                options: [
                    {
                        label: '10',
                        value: 10
                    },
                    {
                        label: '20',
                        value: 20
                    },
                    {
                        label: '50',
                        value: 50
                    },
                    {
                        label: '100',
                        value: 100
                    },
                    {
                        label: '200',
                        value: 200
                    }
                ],
                onChange: (pageSize)=>{
                    var _service_params;
                    const params = field.decoratorProps.params || {};
                    params.pageSize = pageSize;
                    field.decoratorProps.params = params;
                    fieldSchema['x-decorator-props']['params'] = params;
                    service.run({
                        ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                        pageSize,
                        page: 1
                    });
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-decorator-props': fieldSchema['x-decorator-props']
                        }
                    });
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsConnectDataBlocks, {
                type: utils_js_namespaceObject.FilterBlockType.TABLE,
                emptyDescription: t('No blocks to connect')
            }),
            supportTemplate && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsDivider, {}),
            supportTemplate && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsTemplate, {
                componentName: 'Table',
                collectionName: name,
                resourceName: defaultResource
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
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
});
