import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import lodash from "lodash";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../application/schema-settings/index.mjs";
import { useFormBlockContext } from "../../../block-provider/index.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
import { VariableInput, getShouldChange } from "../../../schema-settings/index.mjs";
import { SchemaSettingsDataScope } from "../../../schema-settings/SchemaSettingsDataScope.mjs";
import { useLocalVariables, useVariables } from "../../../variables/index.mjs";
import { useDesignable } from "../../hooks/index.mjs";
import { removeNullCondition } from "../filter/index.mjs";
import { useIsFormReadPretty, useIsSelectFieldMode } from "./FormItem.Settings.mjs";
import { EditComponent, EditDescription, EditOperator, EditTitle, EditTitleField, EditTooltip, EditValidationRules } from "./SchemaSettingOptions.mjs";
const filterFormItemSettings = new SchemaSettings({
    name: 'FilterFormItemSettings',
    items: [
        {
            name: 'editFieldTitle',
            Component: EditTitle
        },
        {
            name: "editDescription",
            Component: EditDescription
        },
        {
            name: 'editTooltip',
            Component: EditTooltip
        },
        {
            name: 'validationRules',
            Component: EditValidationRules
        },
        {
            name: 'setDataScope',
            Component: SchemaSettingsDataScope,
            useVisible () {
                const isSelectFieldMode = useIsSelectFieldMode();
                const isFormReadPretty = useIsFormReadPretty();
                return isSelectFieldMode && !isFormReadPretty;
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops_service_params, _fieldSchema_xcomponentprops_service, _fieldSchema_xcomponentprops;
                const { getCollectionJoinField, getAllCollectionsInheritChain } = useCollectionManager_deprecated();
                const { getField } = useCollection_deprecated();
                const { form } = useFormBlockContext();
                const record = useRecord();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                const variables = useVariables();
                const localVariables = useLocalVariables();
                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_service = _fieldSchema_xcomponentprops.service) ? void 0 : null == (_fieldSchema_xcomponentprops_service_params = _fieldSchema_xcomponentprops_service.params) ? void 0 : _fieldSchema_xcomponentprops_service_params.filter) || {},
                    form,
                    dynamicComponent: (props)=>/*#__PURE__*/ jsx(VariableInput, {
                            ...props,
                            form: form,
                            collectionField: props.collectionField,
                            record: record,
                            shouldChange: getShouldChange({
                                collectionField: props.collectionField,
                                variables,
                                localVariables,
                                getAllCollectionsInheritChain
                            })
                        }),
                    onSubmit: (param)=>{
                        let { filter } = param;
                        filter = removeNullCondition(filter);
                        lodash.set(field.componentProps, 'service.params.filter', filter);
                        fieldSchema['x-component-props'] = field.componentProps;
                        dn.emit('patch', {
                            schema: {
                                ['x-uid']: fieldSchema['x-uid'],
                                'x-component-props': field.componentProps
                            }
                        });
                    }
                };
            }
        },
        {
            name: 'fieldMode',
            Component: EditComponent
        },
        {
            name: 'operator',
            Component: EditOperator
        },
        {
            name: 'titleField',
            Component: EditTitleField
        },
        {
            name: 'divider',
            type: 'divider',
            useVisible () {
                const { getCollectionJoinField } = useCollectionManager_deprecated();
                const { getField } = useCollection_deprecated();
                const fieldSchema = useFieldSchema();
                const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
                return !!collectionField;
            }
        },
        {
            name: 'remove',
            type: 'remove',
            useComponentProps () {
                const { t } = useTranslation();
                return {
                    removeParentsIfNoChildren: true,
                    confirm: {
                        title: t('Delete field')
                    },
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                };
            }
        }
    ]
});
export { filterFormItemSettings };
