import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useFormBlockContext } from "../../../../block-provider/index.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { fieldComponentSettingsItem } from "../../../../data-source/index.mjs";
import { useCompile, useDesignable } from "../../../../schema-component/index.mjs";
import { SchemaSettingsDefaultSortingRules } from "../../../../schema-settings/index.mjs";
import { SchemaSettingsDataScope } from "../../../../schema-settings/SchemaSettingsDataScope.mjs";
const filterCollapseItemFieldSettings = new SchemaSettings({
    name: 'fieldSettings:FilterCollapseItem',
    items: [
        {
            name: 'decoratorOptions',
            type: 'itemGroup',
            hideIfNoChildren: true,
            useComponentProps () {
                const { t } = useTranslation();
                return {
                    title: t('Generic properties')
                };
            },
            useChildren () {
                return [
                    {
                        name: 'customTitle',
                        type: 'modal',
                        useComponentProps () {
                            var _collectionField_uiSchema;
                            const fieldSchema = useFieldSchema();
                            const { t } = useTranslation();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            const { dn } = useDesignable();
                            return {
                                title: t('Custom title'),
                                schema: {
                                    type: 'object',
                                    title: t('Custom title'),
                                    properties: {
                                        title: {
                                            default: null == fieldSchema ? void 0 : fieldSchema.title,
                                            description: "".concat(t('Original title: ')).concat((null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : _collectionField_uiSchema.title) || (null == fieldSchema ? void 0 : fieldSchema.title)),
                                            'x-decorator': 'FormItem',
                                            'x-component': 'Input',
                                            'x-component-props': {}
                                        }
                                    }
                                },
                                onSubmit: (param)=>{
                                    let { title } = param;
                                    if (title) {
                                        fieldSchema.title = title;
                                        dn.emit('patch', {
                                            schema: {
                                                'x-uid': fieldSchema['x-uid'],
                                                title: fieldSchema.title
                                            }
                                        });
                                    }
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: 'defaultCollapse',
                        type: 'switch',
                        useComponentProps () {
                            const fieldSchema = useFieldSchema();
                            const field = useField();
                            const { t } = useTranslation();
                            const { dn } = useDesignable();
                            return {
                                title: t('Default collapse'),
                                checked: field.componentProps.defaultCollapse,
                                onChange: (v)=>{
                                    field.componentProps.defaultCollapse = v;
                                    lodash.set(fieldSchema, 'x-component-props.defaultCollapse', v);
                                    dn.emit('patch', {
                                        schema: {
                                            ['x-uid']: fieldSchema['x-uid'],
                                            'x-component-props': fieldSchema['x-component-props']
                                        }
                                    });
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: 'setTheDataScope',
                        Component: SchemaSettingsDataScope,
                        useComponentProps () {
                            var _fieldSchema_xcomponentprops_params, _fieldSchema_xcomponentprops;
                            const fieldSchema = useFieldSchema();
                            const { form } = useFormBlockContext();
                            const field = useField();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            const { dn } = useDesignable();
                            return {
                                collectionName: null == collectionField ? void 0 : collectionField.target,
                                defaultFilter: (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : null == (_fieldSchema_xcomponentprops_params = _fieldSchema_xcomponentprops.params) ? void 0 : _fieldSchema_xcomponentprops_params.filter) || {},
                                form: form,
                                onSubmit: (param)=>{
                                    let { filter } = param;
                                    var _field_componentProps;
                                    lodash.set(field.componentProps, 'params', {
                                        ...null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.params,
                                        filter
                                    });
                                    fieldSchema['x-component-props']['params'] = field.componentProps.params;
                                    dn.emit('patch', {
                                        schema: {
                                            ['x-uid']: fieldSchema['x-uid'],
                                            'x-component-props': fieldSchema['x-component-props']
                                        }
                                    });
                                }
                            };
                        }
                    },
                    {
                        name: 'setDefaultSortingRules',
                        Component: SchemaSettingsDefaultSortingRules,
                        useComponentProps () {
                            const fieldSchema = useFieldSchema();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            return {
                                name: null == collectionField ? void 0 : collectionField.target
                            };
                        }
                    },
                    {
                        name: 'titleField',
                        type: 'select',
                        useComponentProps () {
                            var _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops;
                            const fieldSchema = useFieldSchema();
                            const { t } = useTranslation();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            const { getCollectionFields } = useCollectionManager_deprecated();
                            const compile = useCompile();
                            const { dn } = useDesignable();
                            const targetFields = (null == collectionField ? void 0 : collectionField.target) ? getCollectionFields(null == collectionField ? void 0 : collectionField.target) : [];
                            const options = targetFields.filter((field)=>!(null == field ? void 0 : field.target) && 'boolean' !== field.type).map((field)=>{
                                var _field_uiSchema;
                                return {
                                    value: null == field ? void 0 : field.name,
                                    label: compile(null == field ? void 0 : null == (_field_uiSchema = field.uiSchema) ? void 0 : _field_uiSchema.title) || (null == field ? void 0 : field.name)
                                };
                            });
                            const onTitleFieldChange = (label)=>{
                                var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema, _fieldSchema_xcomponentprops;
                                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'],
                                    ...null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops['fieldNames'],
                                    label
                                };
                                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                                fieldSchema['x-component-props']['fieldNames'] = fieldNames;
                                schema['x-component-props'] = fieldSchema['x-component-props'];
                                dn.emit('patch', {
                                    schema
                                });
                                dn.refresh();
                            };
                            return {
                                key: 'title-field',
                                title: t('Title field'),
                                options: options,
                                value: null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : null == (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops.fieldNames) ? void 0 : _fieldSchema_xcomponentprops_fieldNames.label,
                                onChange: onTitleFieldChange
                            };
                        }
                    },
                    {
                        ...fieldComponentSettingsItem
                    }
                ];
            }
        },
        {
            name: 'divider',
            type: 'divider'
        },
        {
            name: 'delete',
            type: 'remove',
            sort: 100,
            useComponentProps () {
                const { t } = useTranslation();
                return {
                    removeParentsIfNoChildren: true,
                    confirm: {
                        title: t('Delete field')
                    },
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                };
            }
        }
    ]
});
export { filterCollapseItemFieldSettings };
