import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useCollectionManager_deprecated } from "../../../../collection-manager/index.mjs";
import { useDataSourceManager } from "../../../../data-source/index.mjs";
import { useCompile, useDesignable } from "../../../../schema-component/index.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
const multipleComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:multiple',
    items: [
        {
            name: 'fieldComponent',
            type: 'select',
            useComponentProps () {
                var _collectionInterface_componentOptions_filter, _collectionInterface_componentOptions, _fieldSchema_xcomponentprops;
                const { t } = useTranslation();
                const field = useField();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const { getCollectionJoinField } = useCollectionManager_deprecated();
                const dm = useDataSourceManager();
                const schema = useFieldSchema();
                const fieldSchema = tableColumnSchema || schema;
                const collectionField = getCollectionJoinField(fieldSchema['x-collection-field']);
                const collectionInterface = dm.collectionFieldInterfaceManager.getFieldInterface(null == collectionField ? void 0 : collectionField.interface);
                const compile = useCompile();
                const fieldModeOptions = [
                    {
                        label: t('Select'),
                        value: 'multiple'
                    }
                ];
                null == collectionInterface || null == (_collectionInterface_componentOptions = collectionInterface.componentOptions) || null == (_collectionInterface_componentOptions_filter = _collectionInterface_componentOptions.filter((item)=>!item.useVisible || item.useVisible())) || _collectionInterface_componentOptions_filter.forEach((item)=>{
                    if (!(null == fieldModeOptions ? void 0 : fieldModeOptions.find((modeItem)=>modeItem.value === item.value))) null == fieldModeOptions || fieldModeOptions.push({
                        label: compile(item.label),
                        value: item.value
                    });
                });
                const { dn } = useDesignable();
                return {
                    title: t('Field component'),
                    options: fieldModeOptions,
                    value: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.mode) || 'multiple',
                    onChange (mode) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['mode'] = mode;
                        fieldSchema['x-component-props']['multiple'] = true;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        field.componentProps = field.componentProps || {};
                        field.componentProps.mode = mode;
                        field.componentProps.multiple = true;
                        dn.emit('patch', {
                            schema
                        });
                        dn.refresh();
                    }
                };
            }
        }
    ]
});
export { multipleComponentFieldSettings };
