import { ArrayItems } from "@tachybase/components";
import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useFormBlockContext } from "../../../../block-provider/index.mjs";
import { useCollection_deprecated, useSortFields } from "../../../../collection-manager/index.mjs";
import { removeNullCondition, useDesignable } from "../../../../schema-component/index.mjs";
import { SchemaSettingsBlockTitleItem, SchemaSettingsTemplate } from "../../../../schema-settings/index.mjs";
import { SchemaSettingsDataScope } from "../../../../schema-settings/SchemaSettingsDataScope.mjs";
import { SetDataLoadingMode } from "../details-multi/setDataLoadingModeSettingsItem.mjs";
const listBlockSettings = new SchemaSettings({
    name: 'blockSettings:list',
    items: [
        {
            name: 'EditBlockTitle',
            Component: SchemaSettingsBlockTitleItem
        },
        {
            name: 'SetTheDataScope',
            Component: SchemaSettingsDataScope,
            useComponentProps () {
                var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops;
                const { name } = useCollection_deprecated();
                const fieldSchema = useFieldSchema();
                const { form } = useFormBlockContext();
                const field = useField();
                const { dn } = useDesignable();
                return {
                    collectionName: name,
                    defaultFilter: (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : null == (_fieldSchema_xdecoratorprops_params = _fieldSchema_xdecoratorprops.params) ? void 0 : _fieldSchema_xdecoratorprops_params.filter) || {},
                    form: form,
                    onSubmit: (param)=>{
                        let { filter } = param;
                        filter = removeNullCondition(filter);
                        lodash.set(fieldSchema, 'x-decorator-props.params.filter', filter);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params,
                            page: 1
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'SetDefaultSortingRules',
            type: 'modal',
            useComponentProps () {
                var _fieldSchema_xdecoratorprops_params, _fieldSchema_xdecoratorprops;
                const { name } = useCollection_deprecated();
                const { t } = useTranslation();
                const fieldSchema = useFieldSchema();
                const field = useField();
                const { dn } = useDesignable();
                const sortFields = useSortFields(name);
                const defaultSort = (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : null == (_fieldSchema_xdecoratorprops_params = _fieldSchema_xdecoratorprops.params) ? void 0 : _fieldSchema_xdecoratorprops_params.sort) || [];
                const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>item.startsWith('-') ? {
                        field: item.substring(1),
                        direction: 'desc'
                    } : {
                        field: item,
                        direction: 'asc'
                    });
                return {
                    title: t('Set default sorting rules'),
                    components: {
                        ArrayItems: 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,
                                                    'x-decorator': 'FormItem',
                                                    'x-component': 'Select',
                                                    'x-component-props': {
                                                        style: {
                                                            width: 260
                                                        },
                                                        optionLabelProp: 'fullLabel'
                                                    }
                                                },
                                                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;
                        const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
                        lodash.set(fieldSchema, 'x-decorator-props.params.sort', sortArr);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params,
                            page: 1
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'setDataLoadingMode',
            Component: SetDataLoadingMode
        },
        {
            name: 'RecordsPerPage',
            type: 'select',
            useComponentProps () {
                var _field_decoratorProps_params, _field_decoratorProps;
                const { t } = useTranslation();
                const fieldSchema = useFieldSchema();
                const field = useField();
                const { dn } = useDesignable();
                return {
                    title: t('Records per page'),
                    value: (null == (_field_decoratorProps = field.decoratorProps) ? void 0 : null == (_field_decoratorProps_params = _field_decoratorProps.params) ? void 0 : _field_decoratorProps_params.pageSize) || 20,
                    options: [
                        {
                            label: '10',
                            value: 10
                        },
                        {
                            label: '20',
                            value: 20
                        },
                        {
                            label: '50',
                            value: 50
                        },
                        {
                            label: '80',
                            value: 80
                        },
                        {
                            label: '100',
                            value: 100
                        }
                    ],
                    onChange: (pageSize)=>{
                        lodash.set(fieldSchema, 'x-decorator-props.params.pageSize', pageSize);
                        field.decoratorProps.params = {
                            ...fieldSchema['x-decorator-props'].params,
                            page: 1
                        };
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-decorator-props': fieldSchema['x-decorator-props']
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'ConvertReferenceToDuplicate',
            Component: SchemaSettingsTemplate,
            useComponentProps () {
                var _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1;
                const { name } = useCollection_deprecated();
                const fieldSchema = useFieldSchema();
                const defaultResource = (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops.resource) || (null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops1.association);
                return {
                    componentName: 'List',
                    collectionName: name,
                    resourceName: defaultResource
                };
            }
        },
        {
            name: 'divider',
            type: 'divider'
        },
        {
            name: 'delete',
            type: 'remove',
            useComponentProps () {
                return {
                    removeParentsIfNoChildren: true,
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                };
            }
        }
    ]
});
export { listBlockSettings };
