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, useDataSourceManager } from "../../../../data-source/index.mjs";
import { useCompile, useDesignable, useFieldModeOptions, useIsAddNewForm } from "../../../../schema-component/index.mjs";
import { isSubMode } from "../../../../schema-component/antd/association-field/util.mjs";
import { useIsAssociationField, useIsFieldReadPretty, useIsSelectFieldMode } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import { useIsShowMultipleSwitch } from "../../../../schema-settings/hooks/useIsShowMultipleSwitch.mjs";
import { CustomTitle, allowMultiple, enableLink, quickCreate, setDefaultSortingRules, setTheDataScope } from "../Select/selectComponentFieldSettings.mjs";
const CustomTitleComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:CustomTitle',
    items: [
        {
            name: 'fieldComponent',
            type: 'select',
            useComponentProps () {
                var _collectionInterface_componentOptions_filter, _collectionInterface_componentOptions;
                const { t } = useTranslation();
                const field = useField();
                const collectionFieldCurrent = useCollectionField();
                const { fieldSchema: tableColumnSchema, collectionField } = useColumnSchema();
                const schema = useFieldSchema();
                const fieldSchema = tableColumnSchema || schema;
                const fieldModeOptions = useFieldModeOptions({
                    fieldSchema: tableColumnSchema,
                    collectionField
                });
                const isAddNewForm = useIsAddNewForm();
                const fieldMode = useFieldComponentName();
                const { dn } = useDesignable();
                const isSelect = [
                    'select',
                    'multipleSelect'
                ].includes(null == collectionFieldCurrent ? void 0 : collectionFieldCurrent.interface);
                const optionsMuiltipleSelect = [
                    {
                        label: t('Select'),
                        value: 'Select'
                    },
                    {
                        label: t('Radio group'),
                        value: 'Radio group'
                    },
                    {
                        label: t('Custom Title'),
                        value: 'CustomTitle'
                    }
                ];
                if ((null == collectionFieldCurrent ? void 0 : collectionFieldCurrent.interface) === 'multipleSelect') {
                    const index = optionsMuiltipleSelect.findIndex((item)=>{
                        if ('Select' === item.value) item.value = 'multiple';
                        return 'Radio group' === item.value;
                    });
                    optionsMuiltipleSelect.splice(index, 1);
                }
                const dm = useDataSourceManager();
                const collectionInterface = dm.collectionFieldInterfaceManager.getFieldInterface(null == collectionField ? void 0 : collectionField.interface);
                const compile = useCompile();
                const componentOptions = isSelect ? optionsMuiltipleSelect : fieldModeOptions;
                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 == componentOptions ? void 0 : componentOptions.find((modeItem)=>modeItem.value === item.value))) null == componentOptions || componentOptions.push({
                        label: compile(item.label),
                        value: item.value
                    });
                });
                return {
                    title: t('Field component'),
                    options: componentOptions,
                    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) {
                            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();
                    }
                };
            },
            useVisible () {
                const fieldMode = useFieldComponentName();
                return 'CustomTitle' === fieldMode;
            }
        },
        {
            ...CustomTitle,
            useVisible () {
                const fieldMode = useFieldComponentName();
                return 'CustomTitle' === fieldMode;
            }
        },
        {
            ...setTheDataScope,
            useVisible () {
                const isSelectFieldMode = useIsSelectFieldMode();
                const isFieldReadPretty = useIsFieldReadPretty();
                return isSelectFieldMode && !isFieldReadPretty;
            }
        },
        {
            ...setDefaultSortingRules,
            useComponentProps () {
                const { fieldSchema } = useColumnSchema();
                return {
                    fieldSchema
                };
            },
            useVisible () {
                const isSelectFieldMode = useIsSelectFieldMode();
                const isFieldReadPretty = useIsFieldReadPretty();
                return isSelectFieldMode && !isFieldReadPretty;
            }
        },
        {
            ...quickCreate,
            useVisible () {
                const isAssociationField = useIsAssociationField();
                const readPretty = useIsFieldReadPretty();
                const { fieldSchema } = useColumnSchema();
                return isAssociationField && !fieldSchema && !readPretty;
            }
        },
        {
            ...allowMultiple,
            useVisible () {
                const isAssociationField = useIsAssociationField();
                const IsShowMultipleSwitch = useIsShowMultipleSwitch();
                return isAssociationField && IsShowMultipleSwitch();
            }
        },
        {
            ...enableLink,
            useVisible () {
                const readPretty = useIsFieldReadPretty();
                return useIsAssociationField() && readPretty;
            }
        }
    ]
});
export { CustomTitleComponentFieldSettings };
