import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__ from "../../../../application/schema-settings/SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__ from "../../../../filter-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_page_FixedBlockDesignerItem_mjs__ from "../../../../schema-component/antd/page/FixedBlockDesignerItem.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__details_multi_setDataLoadingModeSettingsItem_mjs__ from "../details-multi/setDataLoadingModeSettingsItem.mjs";
const tableBlockSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'blockSettings:table',
    items: [
        {
            name: 'editBlockTitle',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsBlockTitleItem
        },
        {
            name: 'treeTable',
            type: 'switch',
            useComponentProps: ()=>{
                var _getCollection;
                const { getCollectionField, getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
                const { 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);
                return {
                    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);
                    }
                };
            },
            useVisible: ()=>{
                const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
                const { resource } = field.decoratorProps;
                const collectionField = resource && getCollectionField(resource);
                return (null == collection ? void 0 : collection.tree) && (null == collectionField ? void 0 : collectionField.collectionName) === (null == collectionField ? void 0 : collectionField.target);
            }
        },
        {
            name: 'enableDragAndDropSorting',
            type: 'switch',
            useComponentProps: ()=>{
                const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                const { resource } = field.decoratorProps;
                const collectionField = resource && getCollectionField(resource);
                const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
                return {
                    title: t('Enable drag and drop sorting'),
                    checked: field.decoratorProps.dragSort,
                    onChange: async (dragSort)=>{
                        var _service_params;
                        if (dragSort && collectionField) {
                            var _data_data_, _data_data;
                            const { data } = await api.resource('collections.fields', collectionField.collectionName).update({
                                filterByTk: collectionField.name,
                                values: {
                                    sortable: true
                                }
                            });
                            const sortBy = null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : null === (_data_data_ = _data_data[0]) || void 0 === _data_data_ ? void 0 : _data_data_.sortBy;
                            fieldSchema['x-decorator-props'].dragSortBy = sortBy;
                        }
                        field.decoratorProps.dragSort = dragSort;
                        fieldSchema['x-decorator-props'].dragSort = dragSort;
                        service.run({
                            ...null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                            sort: fieldSchema['x-decorator-props'].dragSortBy
                        });
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'SortField',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsSortField,
            useVisible () {
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                return field.decoratorProps.dragSort;
            }
        },
        {
            name: 'FixBlock',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_page_FixedBlockDesignerItem_mjs__.FixedBlockDesignerItem
        },
        {
            name: 'SetTheDataScope',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDataScope,
            useComponentProps: ()=>{
                var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops;
                const { name } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
                const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
                const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                const dataLoadingMode = (0, __WEBPACK_EXTERNAL_MODULE__details_multi_setDataLoadingModeSettingsItem_mjs__.useDataLoadingMode)();
                const onDataScopeSubmit = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
                    let { filter } = param;
                    var _service_params_, _service_params;
                    filter = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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) || {};
                    if ('auto' === dataLoadingMode) {
                        var _service_params1;
                        service.run({
                            ...null === (_service_params1 = service.params) || void 0 === _service_params1 ? void 0 : _service_params1[0],
                            filter: (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.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
                ]);
                return {
                    collectionName: name,
                    defaultFilter: (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.filter) || {},
                    form: form,
                    onSubmit: onDataScopeSubmit
                };
            }
        },
        {
            name: 'SetDefaultSortingRules',
            type: 'modal',
            useComponentProps () {
                var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops;
                const { name } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useSortFields)(name);
                const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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 sort = null == defaultSort ? void 0 : defaultSort.map((item)=>(null == item ? void 0 : item.startsWith('-')) ? {
                        field: item.substring(1),
                        direction: 'desc'
                    } : {
                        field: item,
                        direction: 'asc'
                    });
                return {
                    title: t('Set default sorting rules'),
                    components: {
                        ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.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
                        });
                    }
                };
            },
            useVisible () {
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const { dragSort } = field.decoratorProps;
                return !dragSort;
            }
        },
        {
            name: 'setDataLoadingMode',
            Component: __WEBPACK_EXTERNAL_MODULE__details_multi_setDataLoadingModeSettingsItem_mjs__.SetDataLoadingMode
        },
        {
            name: 'RecordsPerPage',
            type: 'select',
            useComponentProps () {
                var _field_decoratorProps_params, _field_decoratorProps;
                const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useTableBlockContext)();
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                return {
                    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']
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'ConnectDataBlocks',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsConnectDataBlocks,
            useComponentProps () {
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                return {
                    type: __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.FilterBlockType.TABLE,
                    emptyDescription: t('No blocks to connect')
                };
            }
        },
        {
            name: 'divider',
            type: 'divider',
            useVisible: ()=>{
                var _fieldSchema_xdecoratorprops;
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const supportTemplate = !(null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops ? void 0 : _fieldSchema_xdecoratorprops.disableTemplate);
                return supportTemplate;
            }
        },
        {
            name: 'ConvertReferenceToDuplicate',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsTemplate,
            useComponentProps () {
                var _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1;
                const { name } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const defaultResource = (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops ? void 0 : _fieldSchema_xdecoratorprops.resource) || (null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops1 ? void 0 : _fieldSchema_xdecoratorprops1.association);
                return {
                    componentName: 'Table',
                    collectionName: name,
                    resourceName: defaultResource
                };
            },
            useVisible: ()=>{
                var _fieldSchema_xdecoratorprops;
                const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                const supportTemplate = !(null == fieldSchema ? void 0 : null === (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) || void 0 === _fieldSchema_xdecoratorprops ? void 0 : _fieldSchema_xdecoratorprops.disableTemplate);
                return supportTemplate;
            }
        },
        {
            name: 'divider2',
            type: 'divider'
        },
        {
            name: 'delete',
            type: 'remove',
            useComponentProps: ()=>({
                    removeParentsIfNoChildren: true,
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                })
        }
    ]
});
export { tableBlockSettings };
