import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { useSchemaToolbar } from "../application/schema-toolbar/index.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../collection-manager/index.mjs";
import { useDesignable } from "../schema-component/index.mjs";
import { SchemaSettingsDefaultValue } from "../schema-settings/SchemaSettingsDefaultValue.mjs";
const generalSettingsItems = [
    {
        name: 'editFieldTitle',
        type: 'modal',
        useComponentProps () {
            var _collectionField_uiSchema;
            const { t } = useTranslation();
            const { dn } = useDesignable();
            const field = useField();
            const fieldSchema = useFieldSchema();
            const { getCollectionJoinField } = useCollectionManager_deprecated();
            const { getField } = useCollection_deprecated();
            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
            return {
                title: t('Edit field title'),
                schema: {
                    type: 'object',
                    title: t('Edit field title'),
                    properties: {
                        title: {
                            title: t('Field title'),
                            default: null == field ? void 0 : field.title,
                            description: "".concat(t('Original field title: ')).concat(null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : _collectionField_uiSchema.title),
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            'x-component-props': {}
                        }
                    }
                },
                onSubmit (param) {
                    let { title } = param;
                    if (title) {
                        field.title = title;
                        fieldSchema.title = title;
                        dn.emit('patch', {
                            schema: {
                                'x-uid': fieldSchema['x-uid'],
                                title: fieldSchema.title
                            }
                        });
                    }
                    dn.refresh();
                }
            };
        },
        useVisible () {
            const fieldSchema = useFieldSchema();
            const { getCollectionJoinField } = useCollectionManager_deprecated();
            const { getField } = useCollection_deprecated();
            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
            return !!collectionField;
        }
    },
    {
        name: 'displayTitle',
        type: 'switch',
        useComponentProps () {
            var _fieldSchema_xdecoratorprops;
            const { t } = useTranslation();
            const { dn } = useDesignable();
            const field = useField();
            const fieldSchema = useFieldSchema();
            var _fieldSchema_xdecoratorprops_showTitle;
            return {
                title: t('Display title'),
                checked: null != (_fieldSchema_xdecoratorprops_showTitle = null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops['showTitle']) ? _fieldSchema_xdecoratorprops_showTitle : true,
                onChange (checked) {
                    fieldSchema['x-decorator-props'] = fieldSchema['x-decorator-props'] || {};
                    fieldSchema['x-decorator-props']['showTitle'] = checked;
                    field.decoratorProps.showTitle = checked;
                    dn.emit('patch', {
                        schema: {
                            'x-uid': fieldSchema['x-uid'],
                            'x-decorator-props': {
                                ...fieldSchema['x-decorator-props'],
                                showTitle: checked
                            }
                        }
                    });
                    dn.refresh();
                }
            };
        }
    },
    {
        name: "editDescription",
        type: 'modal',
        useComponentProps () {
            const { t } = useTranslation();
            const { dn } = useDesignable();
            const field = useField();
            const fieldSchema = useFieldSchema();
            return {
                title: t("Edit description"),
                schema: {
                    type: 'object',
                    title: t("Edit description"),
                    properties: {
                        description: {
                            default: null == field ? void 0 : field.description,
                            'x-decorator': 'FormItem',
                            'x-component': 'Input.TextArea',
                            'x-component-props': {}
                        }
                    }
                },
                onSubmit (param) {
                    let { description } = param;
                    field.description = description;
                    fieldSchema.description = description;
                    dn.emit('patch', {
                        schema: {
                            'x-uid': fieldSchema['x-uid'],
                            description: fieldSchema.description
                        }
                    });
                    dn.refresh();
                }
            };
        },
        useVisible () {
            const field = useField();
            return !field.readPretty;
        }
    },
    {
        name: 'editTooltip',
        type: 'modal',
        useComponentProps () {
            var _fieldSchema_xdecoratorprops;
            const { t } = useTranslation();
            const { dn } = useDesignable();
            const field = useField();
            const fieldSchema = useFieldSchema();
            return {
                title: t('Edit tooltip'),
                schema: {
                    type: 'object',
                    title: t('Edit tooltip'),
                    properties: {
                        tooltip: {
                            default: null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops.tooltip,
                            'x-decorator': 'FormItem',
                            'x-component': 'Input.TextArea',
                            'x-component-props': {}
                        }
                    }
                },
                onSubmit (param) {
                    let { tooltip } = param;
                    field.decoratorProps.tooltip = tooltip;
                    fieldSchema['x-decorator-props'] = fieldSchema['x-decorator-props'] || {};
                    fieldSchema['x-decorator-props']['tooltip'] = tooltip;
                    dn.emit('patch', {
                        schema: {
                            'x-uid': fieldSchema['x-uid'],
                            'x-decorator-props': fieldSchema['x-decorator-props']
                        }
                    });
                    dn.refresh();
                }
            };
        },
        useVisible () {
            const field = useField();
            return field.readPretty;
        }
    },
    {
        name: 'required',
        type: 'switch',
        useComponentProps () {
            const { t } = useTranslation();
            const field = useField();
            const fieldSchema = useFieldSchema();
            const { dn, refresh } = useDesignable();
            return {
                title: t('Required'),
                checked: fieldSchema.required,
                onChange (required) {
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    field.required = required;
                    fieldSchema['required'] = required;
                    schema['required'] = required;
                    dn.emit('patch', {
                        schema
                    });
                    refresh();
                }
            };
        },
        useVisible () {
            const field = useField();
            const fieldSchema = useFieldSchema();
            const { required = true } = useSchemaToolbar();
            return !field.readPretty && 'FormField' !== fieldSchema['x-component'] && required;
        }
    },
    {
        name: 'editDefaultValue',
        Component: SchemaSettingsDefaultValue
    }
];
export { generalSettingsItems };
