import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { ArrayItems } from "@tego/client";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useCollectionManager_deprecated, useCollection_deprecated, useDesignable, useSortFields } from "../index.mjs";
import { SchemaSettingsModalItem } from "./SchemaSettings.mjs";
const SchemaSettingsSortingRule = function(props) {
    var _fieldSchema_xcomponentprops_service_params, _fieldSchema_xcomponentprops_service, _fieldSchema_xcomponentprops;
    const field = useField();
    const { dn } = useDesignable();
    const { t } = useTranslation();
    const currentSchema = useFieldSchema();
    const { getField } = useCollection_deprecated();
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    var _props_fieldSchema;
    const fieldSchema = null != (_props_fieldSchema = null == props ? void 0 : props.fieldSchema) ? _props_fieldSchema : currentSchema;
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    const sortFields = useSortFields(null == collectionField ? void 0 : collectionField.target);
    const defaultSort = (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : null == (_fieldSchema_xcomponentprops_service = _fieldSchema_xcomponentprops.service) ? void 0 : null == (_fieldSchema_xcomponentprops_service_params = _fieldSchema_xcomponentprops_service.params) ? void 0 : _fieldSchema_xcomponentprops_service_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 /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
        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,
                                        required: true,
                                        '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;
            var _props_onSubmitCallBack;
            const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
            field.query(new RegExp("[0-9]+\\.".concat(fieldSchema.name, "$"))).forEach((item)=>{
                lodash.set(item, 'componentProps.service.params.sort', sortArr);
            });
            lodash.set(fieldSchema, 'x-component-props.service.params.sort', sortArr);
            null == props || null == (_props_onSubmitCallBack = props.onSubmitCallBack) || _props_onSubmitCallBack.call(props, sortArr);
            field.componentProps = fieldSchema['x-component-props'];
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    'x-component-props': fieldSchema['x-component-props']
                }
            });
        }
    });
};
export { SchemaSettingsSortingRule };
