import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
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_items_index_mjs__ from "../../../schema-items/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_GeneralSchemaDesigner_mjs__ from "../../../schema-settings/GeneralSchemaDesigner.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__ from "../../../schema-settings/hooks/useIsAllowToSetDefaultValue.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsShowMultipleSwitch_mjs__ from "../../../schema-settings/hooks/useIsShowMultipleSwitch.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_isPatternDisabled_mjs__ from "../../../schema-settings/isPatternDisabled.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__ from "../../../schema-settings/SchemaSettings.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";
import * as __WEBPACK_EXTERNAL_MODULE__filter_index_mjs__ from "../filter/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__remote_select_index_mjs__ from "../remote-select/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__select_index_mjs__ from "../select/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ReadPretty_mjs__ from "./ReadPretty.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__useServiceOptions_mjs__ from "./useServiceOptions.mjs";
const divWrap = (schema)=>({
        type: 'void',
        'x-component': 'div',
        properties: {
            [schema.name || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: schema
        }
    });
const InternalAssociationSelect = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { fieldNames, objectValue = true } = props;
    const service = (0, __WEBPACK_EXTERNAL_MODULE__useServiceOptions_mjs__["default"])(props);
    (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useFieldTitle)();
    const normalizeValues = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((obj)=>{
        if (!objectValue && 'object' == typeof obj) return obj[fieldNames.value];
        return obj;
    }, [
        objectValue,
        fieldNames.value
    ]);
    const value = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (void 0 === props.value || null === props.value) return;
        if (Array.isArray(props.value)) return props.value.map(normalizeValues);
        return normalizeValues(props.value);
    }, [
        props.value,
        normalizeValues
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__remote_select_index_mjs__.RemoteSelect, {
        ...props,
        objectValue: objectValue,
        value: value,
        service: service
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    dataSource: 'options',
    loading: true
}, (props, field)=>({
        ...props,
        fieldNames: {
            ...__WEBPACK_EXTERNAL_MODULE__select_index_mjs__.defaultFieldNames,
            ...props.fieldNames,
            ...field.componentProps.fieldNames
        },
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.LoadingOutlined, {}) : props.suffixIcon
    })), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__ReadPretty_mjs__.ReadPretty));
const AssociationSelect = InternalAssociationSelect;
AssociationSelect.Designer = function() {
    var _interfaceConfig_validateSchema, _collectionField_uiSchema, _field_componentProps_service_params, _field_componentProps_service, _field_componentProps, _fieldSchema_xcomponentprops, _field_componentProps_service_params1, _field_componentProps_service1, _field_componentProps1, _field_componentProps_fieldNames, _field_componentProps2;
    const { getCollectionFields, getInterface, getCollectionJoinField, getCollection, isTitleField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const tk = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFilterByTk)();
    const { dn, refresh, insertAdjacent } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const IsShowMultipleSwitch = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsShowMultipleSwitch_mjs__.useIsShowMultipleSwitch)();
    const { isAllowToSetDefaultValue } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.useIsAllowToSetDefaultValue)();
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    const fieldComponentOptions = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useFieldComponentOptions)();
    const isSubFormAssociationField = field.address.segments.includes('__form_grid');
    const interfaceConfig = getInterface(null == collectionField ? void 0 : collectionField.interface);
    const validateSchema = null == interfaceConfig ? void 0 : null === (_interfaceConfig_validateSchema = interfaceConfig['validateSchema']) || void 0 === _interfaceConfig_validateSchema ? void 0 : _interfaceConfig_validateSchema.call(interfaceConfig, fieldSchema);
    const originalTitle = null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema.title;
    const targetFields = (null == collectionField ? void 0 : collectionField.target) ? getCollectionFields(null == collectionField ? void 0 : collectionField.target) : [];
    const initialValue = {
        title: field.title === originalTitle ? void 0 : field.title
    };
    const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useSortFields)(null == collectionField ? void 0 : collectionField.target);
    const defaultSort = (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_service = _field_componentProps.service) || void 0 === _field_componentProps_service ? void 0 : null === (_field_componentProps_service_params = _field_componentProps_service.params) || void 0 === _field_componentProps_service_params ? void 0 : _field_componentProps_service_params.sort) || [];
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (field.readPretty !== fieldSchema['x-read-pretty']) field.readPretty = !!fieldSchema['x-read-pretty'];
    }, [
        fieldSchema['x-read-pretty']
    ]);
    const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>item.startsWith('-') ? {
            field: item.substring(1),
            direction: 'desc'
        } : {
            field: item,
            direction: 'asc'
        });
    if (!field.readPretty) initialValue['required'] = field.required;
    const options = targetFields.filter((field)=>isTitleField(field)).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)
        };
    });
    let readOnlyMode = 'editable';
    if (true === fieldSchema['x-disabled']) readOnlyMode = 'readonly';
    if (true === fieldSchema['x-read-pretty']) readOnlyMode = 'read-pretty';
    const fieldSchemaWithoutRequired = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].omit(fieldSchema, 'required');
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_GeneralSchemaDesigner_mjs__.GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_items_index_mjs__.GeneralSchemaItems, {}),
            form && !(null == form ? void 0 : form.readPretty) && validateSchema && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set validation rules'),
                components: {
                    ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
                    FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.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(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].pickBy(rule, __WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].concat([], (null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _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();
                }
            }),
            isAllowToSetDefaultValue() && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set default value'),
                components: {
                    ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
                    FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
                },
                width: 800,
                schema: {
                    type: 'object',
                    title: t('Set default value'),
                    properties: {
                        default: {
                            ...fieldSchemaWithoutRequired || {},
                            'x-decorator': 'FormItem',
                            'x-component-props': {
                                ...fieldSchemaWithoutRequired['x-component-props'],
                                component: (null == collectionField ? void 0 : collectionField.target) ? 'AssociationSelect' : void 0,
                                service: {
                                    resource: null == collectionField ? void 0 : collectionField.target
                                }
                            },
                            name: 'default',
                            title: t('Default value'),
                            default: fieldSchema.default || (null == collectionField ? void 0 : collectionField.defaultValue),
                            'x-read-pretty': false,
                            'x-disabled': false
                        }
                    }
                },
                onSubmit: (v)=>{
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    if (field.value !== v.default) field.value = v.default;
                    fieldSchema.default = v.default;
                    schema.default = v.default;
                    dn.emit('patch', {
                        schema
                    });
                    refresh();
                }
            }),
            form && !isSubFormAssociationField && fieldComponentOptions && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsSelectItem, {
                title: t('Field component'),
                options: fieldComponentOptions,
                value: fieldSchema['x-component'],
                onChange: (type)=>{
                    var _collectionField_uiSchema, _interfaceConfig_schemaInitialize;
                    const schema = {
                        name: null == collectionField ? void 0 : collectionField.name,
                        type: 'void',
                        required: fieldSchema['required'],
                        description: fieldSchema['description'],
                        default: fieldSchema['default'],
                        'x-decorator': 'FormItem',
                        'x-toolbar': 'FormItemSchemaToolbar',
                        'x-settings': 'fieldSettings:FormItem',
                        'x-component': type,
                        'x-validator': fieldSchema['x-validator'],
                        'x-collection-field': fieldSchema['x-collection-field'],
                        'x-decorator-props': fieldSchema['x-decorator-props'],
                        'x-component-props': {
                            ...null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema['x-component-props'],
                            ...fieldSchema['x-component-props']
                        }
                    };
                    null == interfaceConfig || null === (_interfaceConfig_schemaInitialize = interfaceConfig.schemaInitialize) || void 0 === _interfaceConfig_schemaInitialize || _interfaceConfig_schemaInitialize.call(interfaceConfig, schema, {
                        field: collectionField,
                        block: 'Form',
                        readPretty: field.readPretty,
                        action: tk ? 'get' : null,
                        targetCollection: getCollection(null == collectionField ? void 0 : collectionField.target)
                    });
                    if ('CollectionField' === type) schema['type'] = 'string';
                    insertAdjacent('beforeBegin', divWrap(schema), {
                        onSuccess: ()=>{
                            dn.remove(null, {
                                removeParentsIfNoChildren: true,
                                breakRemoveOn: {
                                    'x-component': 'Grid'
                                }
                            });
                        }
                    });
                }
            }),
            IsShowMultipleSwitch() ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsSwitchItem, {
                title: t('Allow multiple'),
                checked: (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.multiple) === void 0 || fieldSchema['x-component-props'].multiple,
                onChange: (value)=>{
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    field.componentProps = field.componentProps || {};
                    fieldSchema['x-component-props'].multiple = value;
                    field.componentProps.multiple = value;
                    schema['x-component-props'] = fieldSchema['x-component-props'];
                    dn.emit('patch', {
                        schema
                    });
                    refresh();
                }
            }, "multiple") : null,
            /*#__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 === (_field_componentProps1 = field.componentProps) || void 0 === _field_componentProps1 ? void 0 : null === (_field_componentProps_service1 = _field_componentProps1.service) || void 0 === _field_componentProps_service1 ? void 0 : null === (_field_componentProps_service_params1 = _field_componentProps_service1.params) || void 0 === _field_componentProps_service_params1 ? void 0 : _field_componentProps_service_params1.filter) || {},
                form: form,
                onSubmit: (param)=>{
                    let { filter } = param;
                    filter = (0, __WEBPACK_EXTERNAL_MODULE__filter_index_mjs__.removeNullCondition)(filter);
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].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
                        }
                    });
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set default sorting rules'),
                components: {
                    ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayItems
                },
                schema: {
                    type: 'object',
                    title: t('Set default sorting rules'),
                    properties: {
                        sort: {
                            type: 'array',
                            default: sort,
                            'x-component': 'ArrayItems',
                            'x-decorator': 'FormItem',
                            items: {
                                type: 'object',
                                properties: {
                                    space: {
                                        type: 'void',
                                        'x-component': 'Space',
                                        properties: {
                                            sort: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.SortHandle'
                                            },
                                            field: {
                                                type: 'string',
                                                enum: sortFields,
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Select',
                                                'x-component-props': {
                                                    style: {
                                                        width: 260
                                                    }
                                                }
                                            },
                                            direction: {
                                                type: 'string',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Radio.Group',
                                                'x-component-props': {
                                                    optionType: 'button'
                                                },
                                                enum: [
                                                    {
                                                        label: t('ASC'),
                                                        value: 'asc'
                                                    },
                                                    {
                                                        label: t('DESC'),
                                                        value: 'desc'
                                                    }
                                                ]
                                            },
                                            remove: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.Remove'
                                            }
                                        }
                                    }
                                }
                            },
                            properties: {
                                add: {
                                    type: 'void',
                                    title: t('Add sort field'),
                                    'x-component': 'ArrayItems.Addition'
                                }
                            }
                        }
                    }
                },
                onSubmit: (param)=>{
                    let { sort } = param;
                    const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(field.componentProps, 'service.params.sort', sortArr);
                    fieldSchema['x-component-props'] = field.componentProps;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-component-props': field.componentProps
                        }
                    });
                }
            }),
            form && !(null == form ? void 0 : form.readPretty) && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_isPatternDisabled_mjs__.isPatternDisabled)(fieldSchema) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsSelectItem, {
                title: t('Pattern'),
                options: [
                    {
                        label: t('Editable'),
                        value: 'editable'
                    },
                    {
                        label: t('Readonly'),
                        value: 'readonly'
                    },
                    {
                        label: t('Easy-reading'),
                        value: 'read-pretty'
                    }
                ],
                value: readOnlyMode,
                onChange: (v)=>{
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    switch(v){
                        case 'readonly':
                            fieldSchema['x-read-pretty'] = false;
                            fieldSchema['x-disabled'] = true;
                            schema['x-read-pretty'] = false;
                            schema['x-disabled'] = true;
                            field.readPretty = false;
                            field.disabled = true;
                            break;
                        case 'read-pretty':
                            fieldSchema['x-read-pretty'] = true;
                            fieldSchema['x-disabled'] = false;
                            schema['x-read-pretty'] = true;
                            schema['x-disabled'] = false;
                            field.readPretty = true;
                            break;
                        default:
                            fieldSchema['x-read-pretty'] = false;
                            fieldSchema['x-disabled'] = false;
                            schema['x-read-pretty'] = false;
                            schema['x-disabled'] = false;
                            field.readPretty = false;
                            field.disabled = false;
                            break;
                    }
                    dn.emit('patch', {
                        schema
                    });
                    dn.refresh();
                }
            }, "pattern"),
            (null == collectionField ? void 0 : collectionField.target) && [
                'CollectionField',
                'AssociationSelect'
            ].includes(fieldSchema['x-component']) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsSelectItem, {
                title: t('Title field'),
                options: options,
                value: null == field ? void 0 : null === (_field_componentProps2 = field.componentProps) || void 0 === _field_componentProps2 ? void 0 : null === (_field_componentProps_fieldNames = _field_componentProps2.fieldNames) || void 0 === _field_componentProps_fieldNames ? void 0 : _field_componentProps_fieldNames.label,
                onChange: (label)=>{
                    var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema;
                    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'],
                        ...field.componentProps.fieldNames,
                        label
                    };
                    field.componentProps.fieldNames = fieldNames;
                    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();
                }
            }, "title-field"),
            collectionField && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                confirm: {
                    title: t('Delete field')
                },
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            }, "remove")
        ]
    });
};
AssociationSelect.FilterDesigner = function() {
    var _interfaceConfig_validateSchema, _collectionField_uiSchema, _field_componentProps_service_params, _field_componentProps_service, _field_componentProps, _collectionField_uiSchema1, _field_componentProps_service_params1, _field_componentProps_service1, _field_componentProps1, _field_componentProps_fieldNames, _field_componentProps2;
    const { getCollectionFields, getInterface, getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { dn, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    const interfaceConfig = getInterface(null == collectionField ? void 0 : collectionField.interface);
    const validateSchema = null == interfaceConfig ? void 0 : null === (_interfaceConfig_validateSchema = interfaceConfig['validateSchema']) || void 0 === _interfaceConfig_validateSchema ? void 0 : _interfaceConfig_validateSchema.call(interfaceConfig, fieldSchema);
    const originalTitle = null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema.title;
    const targetFields = (null == collectionField ? void 0 : collectionField.target) ? getCollectionFields(null == collectionField ? void 0 : collectionField.target) : [];
    const initialValue = {
        title: field.title === originalTitle ? void 0 : field.title
    };
    const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useSortFields)(null == collectionField ? void 0 : collectionField.target);
    const defaultSort = (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_service = _field_componentProps.service) || void 0 === _field_componentProps_service ? void 0 : null === (_field_componentProps_service_params = _field_componentProps_service.params) || void 0 === _field_componentProps_service_params ? void 0 : _field_componentProps_service_params.sort) || [];
    const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>item.startsWith('-') ? {
            field: item.substring(1),
            direction: 'desc'
        } : {
            field: item,
            direction: 'asc'
        });
    if (!field.readPretty) initialValue['required'] = field.required;
    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)
        };
    });
    if (true === fieldSchema['x-disabled']) 'readonly';
    if (true === fieldSchema['x-read-pretty']) 'read-pretty';
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_GeneralSchemaDesigner_mjs__.GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_items_index_mjs__.GeneralSchemaItems, {
                required: false
            }),
            form && !(null == form ? void 0 : form.readPretty) && validateSchema && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set validation rules'),
                components: {
                    ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
                    FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.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(__WEBPACK_EXTERNAL_MODULE_lodash__["default"].pickBy(rule, __WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].concat([], (null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _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();
                }
            }),
            form && !(null == form ? void 0 : form.readPretty) && (null == collectionField ? void 0 : null === (_collectionField_uiSchema1 = collectionField.uiSchema) || void 0 === _collectionField_uiSchema1 ? void 0 : _collectionField_uiSchema1.type) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set default value'),
                components: {
                    ArrayCollapse: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayCollapse,
                    FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
                },
                schema: {
                    type: 'object',
                    title: t('Set default value'),
                    properties: {
                        default: {
                            ...__WEBPACK_EXTERNAL_MODULE_lodash__["default"].omit(null == collectionField ? void 0 : collectionField.uiSchema, 'required'),
                            name: 'default',
                            title: t('Default value'),
                            'x-decorator': 'FormItem',
                            default: fieldSchema.default || (null == collectionField ? void 0 : collectionField.defaultValue)
                        }
                    }
                },
                onSubmit: (v)=>{
                    const schema = {
                        ['x-uid']: fieldSchema['x-uid']
                    };
                    if (field.value !== v.default) field.value = v.default;
                    fieldSchema.default = v.default;
                    schema.default = v.default;
                    dn.emit('patch', {
                        schema
                    });
                    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 === (_field_componentProps1 = field.componentProps) || void 0 === _field_componentProps1 ? void 0 : null === (_field_componentProps_service1 = _field_componentProps1.service) || void 0 === _field_componentProps_service1 ? void 0 : null === (_field_componentProps_service_params1 = _field_componentProps_service1.params) || void 0 === _field_componentProps_service_params1 ? void 0 : _field_componentProps_service_params1.filter) || {},
                form: form,
                onSubmit: (param)=>{
                    let { filter } = param;
                    filter = (0, __WEBPACK_EXTERNAL_MODULE__filter_index_mjs__.removeNullCondition)(filter);
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].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
                        }
                    });
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsModalItem, {
                title: t('Set default sorting rules'),
                components: {
                    ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayItems
                },
                schema: {
                    type: 'object',
                    title: t('Set default sorting rules'),
                    properties: {
                        sort: {
                            type: 'array',
                            default: sort,
                            'x-component': 'ArrayItems',
                            'x-decorator': 'FormItem',
                            items: {
                                type: 'object',
                                properties: {
                                    space: {
                                        type: 'void',
                                        'x-component': 'Space',
                                        properties: {
                                            sort: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.SortHandle'
                                            },
                                            field: {
                                                type: 'string',
                                                enum: sortFields,
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Select',
                                                'x-component-props': {
                                                    style: {
                                                        width: 260
                                                    }
                                                }
                                            },
                                            direction: {
                                                type: 'string',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'Radio.Group',
                                                'x-component-props': {
                                                    optionType: 'button'
                                                },
                                                enum: [
                                                    {
                                                        label: t('ASC'),
                                                        value: 'asc'
                                                    },
                                                    {
                                                        label: t('DESC'),
                                                        value: 'desc'
                                                    }
                                                ]
                                            },
                                            remove: {
                                                type: 'void',
                                                'x-decorator': 'FormItem',
                                                'x-component': 'ArrayItems.Remove'
                                            }
                                        }
                                    }
                                }
                            },
                            properties: {
                                add: {
                                    type: 'void',
                                    title: t('Add sort field'),
                                    'x-component': 'ArrayItems.Addition'
                                }
                            }
                        }
                    }
                },
                onSubmit: (param)=>{
                    let { sort } = param;
                    const sortArr = sort.map((item)=>'desc' === item.direction ? "-".concat(item.field) : item.field);
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(field.componentProps, 'service.params.sort', sortArr);
                    fieldSchema['x-component-props'] = field.componentProps;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-component-props': field.componentProps
                        }
                    });
                }
            }),
            (null == collectionField ? void 0 : collectionField.target) && [
                'CollectionField',
                'AssociationSelect'
            ].includes(fieldSchema['x-component']) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsSelectItem, {
                title: t('Title field'),
                options: options,
                value: null == field ? void 0 : null === (_field_componentProps2 = field.componentProps) || void 0 === _field_componentProps2 ? void 0 : null === (_field_componentProps_fieldNames = _field_componentProps2.fieldNames) || void 0 === _field_componentProps_fieldNames ? void 0 : _field_componentProps_fieldNames.label,
                onChange: (label)=>{
                    var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema;
                    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'],
                        ...field.componentProps.fieldNames,
                        label
                    };
                    field.componentProps.fieldNames = fieldNames;
                    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();
                }
            }, "title-field"),
            collectionField && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettings_mjs__.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                confirm: {
                    title: t('Delete field')
                },
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            }, "remove")
        ]
    });
};
const AssociationSelect_rslib_entry_ = AssociationSelect;
export { AssociationSelect, AssociationSelect_rslib_entry_ as default };
