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 { OpenMode, useDesignable, useFieldModeOptions, useIsAddNewForm } from "../../../../schema-component/index.mjs";
import { isSubMode } from "../../../../schema-component/antd/association-field/util.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 { titleField } from "../Picker/recordPickerComponentFieldSettings.mjs";
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 { fieldSchema: tableColumnSchema, collectionField } = useColumnSchema();
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        const fieldModeOptions = useFieldModeOptions({
            fieldSchema: tableColumnSchema,
            collectionField
        });
        const { dn } = useDesignable();
        const isAddNewForm = useIsAddNewForm();
        const fieldMode = useFieldComponentName();
        return {
            title: t('Field component'),
            options: fieldModeOptions,
            value: fieldMode,
            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) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const drawerSubTableComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:DrawerSubTable',
    items: [
        fieldComponent,
        allowMultiple,
        titleField,
        {
            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']) === 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();
                    }
                };
            }
        }
    ]
});
export { drawerSubTableComponentFieldSettings };
