import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsDataScope_mjs__ from "../../../schema-settings/SchemaSettingsDataScope.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
const AssociationFilterItemDesigner = (props)=>{
    var _collectionField_uiSchema, _fieldSchema_xcomponentprops_params, _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops1;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.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 ? 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 === _collectionField_uiSchema ? void 0 : null === (_collectionField_uiSchema_xcomponentprops = _collectionField_uiSchema['x-component-props']) || void 0 === _collectionField_uiSchema_xcomponentprops ? void 0 : _collectionField_uiSchema_xcomponentprops['fieldNames'],
            ...null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? 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 /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.GeneralSchemaDesigner, {
        ...props,
        disableInitializer: true,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsModalItem, {
                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 ? 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();
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsSwitchItem, {
                title: t('Default collapse'),
                checked: field.componentProps.defaultCollapse,
                onChange: (v)=>{
                    field.componentProps.defaultCollapse = v;
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].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();
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsDataScope_mjs__.SchemaSettingsDataScope, {
                collectionName: null == collectionField ? void 0 : collectionField.target,
                defaultFilter: (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_params = _fieldSchema_xcomponentprops.params) || void 0 === _fieldSchema_xcomponentprops_params ? void 0 : _fieldSchema_xcomponentprops_params.filter) || {},
                form: form,
                onSubmit: (param)=>{
                    let { filter } = param;
                    var _field_componentProps;
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(field.componentProps, 'params', {
                        ...null === (_field_componentProps = field.componentProps) || void 0 === _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']
                        }
                    });
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDefaultSortingRules, {
                name: null == collectionField ? void 0 : collectionField.target
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsSelectItem, {
                title: t('Title field'),
                options: options,
                value: null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : null === (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops1.fieldNames) || void 0 === _fieldSchema_xcomponentprops_fieldNames ? void 0 : _fieldSchema_xcomponentprops_fieldNames.label,
                onChange: onTitleFieldChange
            }, "title-field"),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsRemove, {
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
export { AssociationFilterItemDesigner };
