import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useFieldComponentName } from "../../../../common/useFieldComponentName.mjs";
import { useCollectionField } from "../../../../data-source/index.mjs";
import { useDesignable, useFieldModeOptions, useIsAddNewForm } from "../../../../schema-component/index.mjs";
import { isSubMode } from "../../../../schema-component/antd/association-field/util.mjs";
import { useIsFieldReadPretty, useTitleFieldOptions } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
const titleField = {
    name: 'titleField',
    type: 'select',
    useComponentProps () {
        var _field_componentProps, _fieldSchema_xcomponentprops, _uiSchema_xcomponentprops;
        const { t } = useTranslation();
        const field = useField();
        const { uiSchema, fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema();
        const options = useTitleFieldOptions();
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        const targetCollectionField = useCollectionField();
        const collectionField = tableColumnField || targetCollectionField;
        const { dn } = useDesignable();
        const fieldNames = (null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.fieldNames) || (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['fieldNames']) || (null == uiSchema ? void 0 : null == (_uiSchema_xcomponentprops = uiSchema['x-component-props']) ? void 0 : _uiSchema_xcomponentprops['fieldNames']);
        return {
            title: t('Title field'),
            options,
            value: null == fieldNames ? void 0 : fieldNames.label,
            onChange (label) {
                var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema;
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                const fieldNames = {
                    ...null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : null == (_collectionField_uiSchema_xcomponentprops = _collectionField_uiSchema['x-component-props']) ? void 0 : _collectionField_uiSchema_xcomponentprops['fieldNames'],
                    ...field.componentProps.fieldNames,
                    label
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['fieldNames'] = fieldNames;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps.fieldNames = fieldSchema['x-component-props'].fieldNames;
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const allowMultiple = {
    name: 'allowMultiple',
    type: 'switch',
    useVisible () {
        const isFieldReadPretty = useIsFieldReadPretty();
        const collectionField = useCollectionField();
        return !isFieldReadPretty && [
            'hasMany',
            'belongsToMany'
        ].includes(null == collectionField ? void 0 : collectionField.type);
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = useTranslation();
        const field = useField();
        const fieldSchema = useFieldSchema();
        const { dn, refresh } = useDesignable();
        return {
            title: t('Allow multiple'),
            checked: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.multiple) === void 0 ? true : fieldSchema['x-component-props'].multiple,
            onChange (value) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                field.componentProps = field.componentProps || {};
                fieldSchema['x-component-props'].multiple = value;
                field.componentProps.multiple = value;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                dn.emit('patch', {
                    schema
                });
                refresh();
            }
        };
    }
};
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = useTranslation();
        const field = useField();
        const isAddNewForm = useIsAddNewForm();
        const fieldComponentName = useFieldComponentName();
        const { fieldSchema: tableColumnSchema, collectionField } = useColumnSchema();
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        const fieldModeOptions = useFieldModeOptions({
            fieldSchema: tableColumnSchema,
            collectionField
        });
        const { dn } = useDesignable();
        return {
            title: t('Field component'),
            options: fieldModeOptions,
            value: fieldComponentName,
            onChange (mode) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['mode'] = mode;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps.mode = mode;
                if (isSubMode(fieldSchema) && isAddNewForm) {
                    var _field_setInitialValue, _field_setValue;
                    schema.default = null;
                    fieldSchema.default = null;
                    null == field || null == (_field_setInitialValue = field.setInitialValue) || _field_setInitialValue.call(field, null);
                    null == field || null == (_field_setValue = field.setValue) || _field_setValue.call(field, null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const recordPickerComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:Picker',
    items: [
        fieldComponent,
        {
            name: 'popupSize',
            type: 'select',
            useVisible () {
                const isFieldReadPretty = useIsFieldReadPretty();
                return !isFieldReadPretty;
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
                const { t } = useTranslation();
                const field = useField();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const schema = useFieldSchema();
                const fieldSchema = tableColumnSchema || schema;
                const { dn } = useDesignable();
                var _fieldSchema_xcomponentprops_openSize;
                return {
                    title: t('Popup size'),
                    options: [
                        {
                            label: t('Small'),
                            value: 'small'
                        },
                        {
                            label: t('Middle'),
                            value: 'middle'
                        },
                        {
                            label: t('Large'),
                            value: 'large'
                        }
                    ],
                    value: null != (_fieldSchema_xcomponentprops_openSize = null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['openSize']) ? _fieldSchema_xcomponentprops_openSize : (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops1['openMode']) == 'modal' ? 'large' : 'middle',
                    onChange: (value)=>{
                        field.componentProps.openSize = value;
                        fieldSchema['x-component-props'] = {
                            ...fieldSchema['x-component-props'],
                            openSize: value
                        };
                        dn.emit('patch', {
                            schema: {
                                'x-uid': fieldSchema['x-uid'],
                                'x-component-props': fieldSchema['x-component-props']
                            }
                        });
                        dn.refresh();
                    }
                };
            }
        },
        allowMultiple,
        titleField
    ]
});
export { recordPickerComponentFieldSettings, titleField };
