import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__ from "./SchemaSettings.mjs";
const SchemaSettingsSortingRule = function(props) {
    var _fieldSchema_xcomponentprops_service_params, _fieldSchema_xcomponentprops_service, _fieldSchema_xcomponentprops;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const currentSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollectionManager_deprecated)();
    var _props_fieldSchema;
    const fieldSchema = null !== (_props_fieldSchema = null == props ? void 0 : props.fieldSchema) && void 0 !== _props_fieldSchema ? _props_fieldSchema : currentSchema;
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSortFields)(null == collectionField ? void 0 : collectionField.target);
    const defaultSort = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_service = _fieldSchema_xcomponentprops.service) || void 0 === _fieldSchema_xcomponentprops_service ? void 0 : null === (_fieldSchema_xcomponentprops_service_params = _fieldSchema_xcomponentprops_service.params) || void 0 === _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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettings_mjs__.SchemaSettingsModalItem, {
        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
                                            },
                                            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)=>{
                __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(item, 'componentProps.service.params.sort', sortArr);
            });
            __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(fieldSchema, 'x-component-props.service.params.sort', sortArr);
            null == props || null === (_props_onSubmitCallBack = props.onSubmitCallBack) || void 0 === _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 };
