import { ArrayCollapse, FormLayout } from "@tachybase/components";
import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { useApp } from "../../../../application/index.mjs";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { useFieldComponentName } from "../../../../common/useFieldComponentName.mjs";
import { EditOperator, useDesignable, useValidateSchema } from "../../../../schema-component/index.mjs";
const filterFormItemFieldSettings = new SchemaSettings({
    name: 'fieldSettings:FilterFormItem',
    items: [
        {
            name: 'decoratorOptions',
            type: 'itemGroup',
            hideIfNoChildren: true,
            useComponentProps () {
                const { t } = useTranslation();
                return {
                    title: t('Generic properties')
                };
            },
            useChildren () {
                return [
                    {
                        name: 'editFieldTitle',
                        type: 'modal',
                        useComponentProps () {
                            var _collectionField_uiSchema;
                            const { t } = useTranslation();
                            const { dn } = useDesignable();
                            const field = useField();
                            const fieldSchema = useFieldSchema();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            return {
                                title: t('Edit field title'),
                                schema: {
                                    type: 'object',
                                    title: t('Edit field title'),
                                    properties: {
                                        title: {
                                            title: t('Field title'),
                                            default: null == field ? void 0 : field.title,
                                            description: "".concat(t('Original field title: ')).concat(null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : _collectionField_uiSchema.title),
                                            'x-decorator': 'FormItem',
                                            'x-component': 'Input',
                                            'x-component-props': {}
                                        }
                                    }
                                },
                                onSubmit (param) {
                                    let { title } = param;
                                    if (title) {
                                        field.title = title;
                                        fieldSchema.title = title;
                                        dn.emit('patch', {
                                            schema: {
                                                'x-uid': fieldSchema['x-uid'],
                                                title: fieldSchema.title
                                            }
                                        });
                                    }
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: 'displayTitle',
                        type: 'switch',
                        useComponentProps () {
                            var _fieldSchema_xdecoratorprops;
                            const { t } = useTranslation();
                            const { dn } = useDesignable();
                            const field = useField();
                            const fieldSchema = useFieldSchema();
                            var _fieldSchema_xdecoratorprops_showTitle;
                            return {
                                title: t('Display title'),
                                checked: null != (_fieldSchema_xdecoratorprops_showTitle = null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops['showTitle']) ? _fieldSchema_xdecoratorprops_showTitle : true,
                                onChange (checked) {
                                    fieldSchema['x-decorator-props'] = fieldSchema['x-decorator-props'] || {};
                                    fieldSchema['x-decorator-props']['showTitle'] = checked;
                                    field.decoratorProps.showTitle = checked;
                                    dn.emit('patch', {
                                        schema: {
                                            'x-uid': fieldSchema['x-uid'],
                                            'x-decorator-props': {
                                                ...fieldSchema['x-decorator-props'],
                                                showTitle: checked
                                            }
                                        }
                                    });
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: "editDescription",
                        type: 'modal',
                        useComponentProps () {
                            const { t } = useTranslation();
                            const { dn } = useDesignable();
                            const field = useField();
                            const fieldSchema = useFieldSchema();
                            return {
                                title: t("Edit description"),
                                schema: {
                                    type: 'object',
                                    title: t("Edit description"),
                                    properties: {
                                        description: {
                                            default: null == field ? void 0 : field.description,
                                            'x-decorator': 'FormItem',
                                            'x-component': 'Input.TextArea',
                                            'x-component-props': {}
                                        }
                                    }
                                },
                                onSubmit (param) {
                                    let { description } = param;
                                    field.description = description;
                                    fieldSchema.description = description;
                                    dn.emit('patch', {
                                        schema: {
                                            'x-uid': fieldSchema['x-uid'],
                                            description: fieldSchema.description
                                        }
                                    });
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: 'editTooltip',
                        type: 'modal',
                        useComponentProps () {
                            var _fieldSchema_xdecoratorprops;
                            const { t } = useTranslation();
                            const { dn } = useDesignable();
                            const field = useField();
                            const fieldSchema = useFieldSchema();
                            return {
                                title: t('Edit tooltip'),
                                schema: {
                                    type: 'object',
                                    title: t('Edit tooltip'),
                                    properties: {
                                        tooltip: {
                                            default: null == fieldSchema ? void 0 : null == (_fieldSchema_xdecoratorprops = fieldSchema['x-decorator-props']) ? void 0 : _fieldSchema_xdecoratorprops.tooltip,
                                            'x-decorator': 'FormItem',
                                            'x-component': 'Input.TextArea',
                                            'x-component-props': {}
                                        }
                                    }
                                },
                                onSubmit (param) {
                                    let { tooltip } = param;
                                    field.decoratorProps.tooltip = tooltip;
                                    fieldSchema['x-decorator-props'] = fieldSchema['x-decorator-props'] || {};
                                    fieldSchema['x-decorator-props']['tooltip'] = tooltip;
                                    dn.emit('patch', {
                                        schema: {
                                            'x-uid': fieldSchema['x-uid'],
                                            'x-decorator-props': fieldSchema['x-decorator-props']
                                        }
                                    });
                                    dn.refresh();
                                }
                            };
                        }
                    },
                    {
                        name: 'setValidationRules',
                        type: 'modal',
                        useComponentProps () {
                            const { t } = useTranslation();
                            const field = useField();
                            const fieldSchema = useFieldSchema();
                            const { dn, refresh } = useDesignable();
                            const validateSchema = useValidateSchema();
                            const { getCollectionJoinField } = useCollectionManager_deprecated();
                            const { getField } = useCollection_deprecated();
                            const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                            return {
                                title: t('Set validation rules'),
                                components: {
                                    ArrayCollapse: ArrayCollapse,
                                    FormLayout: FormLayout
                                },
                                schema: {
                                    type: 'object',
                                    title: t('Set validation rules'),
                                    properties: {
                                        rules: {
                                            type: 'array',
                                            default: null == fieldSchema ? void 0 : fieldSchema['x-validator'],
                                            'x-component': 'ArrayCollapse',
                                            'x-decorator': 'FormItem',
                                            'x-component-props': {
                                                accordion: true
                                            },
                                            maxItems: 3,
                                            items: {
                                                type: 'object',
                                                'x-component': 'ArrayCollapse.CollapsePanel',
                                                'x-component-props': {
                                                    header: '{{ t("Validation rule") }}'
                                                },
                                                properties: {
                                                    index: {
                                                        type: 'void',
                                                        'x-component': 'ArrayCollapse.Index'
                                                    },
                                                    layout: {
                                                        type: 'void',
                                                        'x-component': 'FormLayout',
                                                        'x-component-props': {
                                                            labelStyle: {
                                                                marginTop: '6px'
                                                            },
                                                            labelCol: 8,
                                                            wrapperCol: 16
                                                        },
                                                        properties: {
                                                            ...validateSchema,
                                                            message: {
                                                                type: 'string',
                                                                title: '{{ t("Error message") }}',
                                                                'x-decorator': 'FormItem',
                                                                'x-component': 'Input.TextArea',
                                                                'x-component-props': {
                                                                    autoSize: {
                                                                        minRows: 2,
                                                                        maxRows: 2
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    },
                                                    remove: {
                                                        type: 'void',
                                                        'x-component': 'ArrayCollapse.Remove'
                                                    },
                                                    moveUp: {
                                                        type: 'void',
                                                        'x-component': 'ArrayCollapse.MoveUp'
                                                    },
                                                    moveDown: {
                                                        type: 'void',
                                                        'x-component': 'ArrayCollapse.MoveDown'
                                                    }
                                                }
                                            },
                                            properties: {
                                                add: {
                                                    type: 'void',
                                                    title: '{{ t("Add validation rule") }}',
                                                    'x-component': 'ArrayCollapse.Addition',
                                                    'x-reactions': {
                                                        dependencies: [
                                                            'rules'
                                                        ],
                                                        fulfill: {
                                                            state: {
                                                                disabled: '{{$deps[0].length >= 3}}'
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                onSubmit (v) {
                                    var _collectionField_uiSchema;
                                    const rules = [];
                                    for (const rule of v.rules)rules.push(lodash.pickBy(rule, lodash.identity));
                                    const schema = {
                                        ['x-uid']: fieldSchema['x-uid']
                                    };
                                    if ([
                                        'percent'
                                    ].includes(null == collectionField ? void 0 : collectionField.interface)) for (const rule of rules){
                                        if (!!rule.maxValue || !!rule.minValue) rule['percentMode'] = true;
                                        if (rule.percentFormat) rule['percentFormats'] = true;
                                    }
                                    const concatValidator = lodash.concat([], (null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : _collectionField_uiSchema['x-validator']) || [], rules);
                                    field.validator = concatValidator;
                                    fieldSchema['x-validator'] = rules;
                                    schema['x-validator'] = rules;
                                    dn.emit('patch', {
                                        schema
                                    });
                                    refresh();
                                }
                            };
                        },
                        useVisible () {
                            const validateSchema = useValidateSchema();
                            return !!validateSchema;
                        }
                    },
                    {
                        name: 'operator',
                        Component: EditOperator
                    }
                ];
            }
        },
        {
            name: 'componentOptions',
            type: 'itemGroup',
            hideIfNoChildren: true,
            useComponentProps () {
                const { t } = useTranslation();
                return {
                    title: t('Specific properties')
                };
            },
            useChildren () {
                const app = useApp();
                const fieldComponentName = useFieldComponentName();
                const componentSettings = app.schemaSettingsManager.get("fieldSettings:component:".concat(fieldComponentName));
                return (null == componentSettings ? void 0 : componentSettings.items) || [];
            }
        },
        {
            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 { filterFormItemFieldSettings };
