import { useField, useFieldSchema } from "@tachybase/schema";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useDesignable } from "../../../../schema-component/index.mjs";
import { useIsFieldReadPretty } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import { SchemaSettingsNumberFormat } from "../../../../schema-settings/SchemaSettingsNumberFormat.mjs";
const inputNumberComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:InputNumber',
    items: [
        {
            name: 'setSliderMAXNumber',
            type: 'modal',
            useVisible () {
                var _fieldSchema_xcomponentprops;
                const fieldSchema = useFieldSchema();
                return 'Slider' === fieldSchema['x-component'] || (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.component) === 'Slider';
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
                const { t } = useTranslation();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const { dn } = useDesignable();
                return {
                    title: t('MAX'),
                    value: null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.max,
                    schema: {
                        type: 'object',
                        title: t('Edit Slider Max'),
                        properties: {
                            maxNumber: {
                                default: (null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops1.max) || 100,
                                'x-decorator': 'FormItem',
                                'x-component': 'Input',
                                'x-component-props': {}
                            }
                        }
                    },
                    onSubmit (number) {
                        if (Number(number.maxNumber) > (field.componentProps['min'] || 0)) {
                            fieldSchema['x-component-props']['max'] = Number(number.maxNumber);
                            field.componentProps['max'] = Number(number.maxNumber);
                            dn.emit('patch', {
                                schema: {
                                    'x-uid': fieldSchema['x-uid'],
                                    'x-component-props': fieldSchema['x-component-props']
                                }
                            });
                        } else message.warning(t('Cannot be less than or equal to the minimum value'));
                    }
                };
            }
        },
        {
            name: 'setSliderMinNumber',
            type: 'modal',
            useVisible () {
                var _fieldSchema_xcomponentprops;
                const fieldSchema = useFieldSchema();
                return 'Slider' === fieldSchema['x-component'] || (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.component) === 'Slider';
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
                const { t } = useTranslation();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const { dn } = useDesignable();
                return {
                    title: t('MIN'),
                    value: null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.min,
                    schema: {
                        type: 'object',
                        title: t('Edit Slider Min'),
                        properties: {
                            minNumber: {
                                default: (null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops1.min) || 0,
                                'x-decorator': 'FormItem',
                                'x-component': 'Input',
                                'x-component-props': {}
                            }
                        }
                    },
                    onSubmit (number) {
                        if (Number(number.minNumber) < (field.componentProps['max'] || 0)) {
                            fieldSchema['x-component-props']['min'] = Number(number.minNumber);
                            field.componentProps['min'] = Number(number.minNumber);
                            dn.emit('patch', {
                                schema: {
                                    'x-uid': fieldSchema['x-uid'],
                                    'x-component-props': fieldSchema['x-component-props']
                                }
                            });
                        } else message.warning(t('Cannot be greater than or equal to the maximum value'));
                    }
                };
            }
        },
        {
            name: 'displayFormat',
            Component: SchemaSettingsNumberFormat,
            useComponentProps () {
                const schema = useFieldSchema();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const fieldSchema = tableColumnSchema || schema;
                return {
                    fieldSchema
                };
            },
            useVisible () {
                const isFieldReadPretty = useIsFieldReadPretty();
                return isFieldReadPretty;
            }
        }
    ]
});
export { inputNumberComponentFieldSettings };
