import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import "react";
import { observer, useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../collection-manager/index.mjs";
import { useDesignable } from "../schema-component/index.mjs";
import { getTempFieldState } from "../schema-component/antd/form-v2/utils.mjs";
import { SchemaSettingsModalItem, SchemaSettingsSwitchItem } from "../schema-settings/index.mjs";
const GeneralSchemaItems = observer((props)=>{
    var _collectionField_uiSchema, _fieldSchema_xdecoratorprops, _fieldSchema_xdecoratorprops1;
    const { required = true } = props;
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    const { getField } = useCollection_deprecated();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { t } = useTranslation();
    const { dn, refresh } = useDesignable();
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    var _fieldSchema_xdecoratorprops_showTitle;
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            collectionField && /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                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();
                }
            }, "edit-field-title"),
            /*#__PURE__*/ jsx(SchemaSettingsSwitchItem, {
                checked: null != (_fieldSchema_xdecoratorprops_showTitle = null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops['showTitle']) ? _fieldSchema_xdecoratorprops_showTitle : true,
                title: t('Display title'),
                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();
                }
            }),
            !field.readPretty && /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                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();
                }
            }, "edit-description"),
            field.readPretty && /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
                title: t('Edit tooltip'),
                schema: {
                    type: 'object',
                    title: t("Edit description"),
                    properties: {
                        tooltip: {
                            default: null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops1 = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops1.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();
                }
            }, "edit-tooltip"),
            !field.readPretty && 'FormField' !== fieldSchema['x-component'] && required && /*#__PURE__*/ jsx(SchemaSettingsSwitchItem, {
                title: t('Required'),
                checked: fieldSchema.required,
                onChange: (required)=>{
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    field.required = required;
                    lodash.set(field, 'initStateOfLinkageRules.required', getTempFieldState(true, required));
                    fieldSchema['required'] = required;
                    schema['required'] = required;
                    dn.emit('patch', {
                        schema
                    });
                    refresh();
                }
            }, "required")
        ]
    });
}, {
    displayName: 'GeneralSchemaItems'
});
export { GeneralSchemaItems };
