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__application_schema_settings_SchemaSettings_mjs__ from "../../../../application/schema-settings/SchemaSettings.mjs";
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__common_useFieldComponentName_mjs__ from "../../../../common/useFieldComponentName.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_util_mjs__ from "../../../../schema-component/antd/association-field/util.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__ from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__ from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsShowMultipleSwitch_mjs__ from "../../../../schema-settings/hooks/useIsShowMultipleSwitch.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsDataScope_mjs__ from "../../../../schema-settings/SchemaSettingsDataScope.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsSortingRule_mjs__ from "../../../../schema-settings/SchemaSettingsSortingRule.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../../../variables/index.mjs";
const enableLink = {
    name: 'enableLink',
    type: 'switch',
    useVisible () {
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        return field.readPretty;
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { fieldSchema: tableColumnSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            title: t('Enable link'),
            checked: (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.enableLink) !== false,
            onChange (flag) {
                fieldSchema['x-component-props'] = {
                    ...null == fieldSchema ? void 0 : fieldSchema['x-component-props'],
                    enableLink: flag
                };
                field.componentProps['enableLink'] = flag;
                dn.emit('patch', {
                    schema: {
                        'x-uid': fieldSchema['x-uid'],
                        'x-component-props': {
                            ...null == fieldSchema ? void 0 : fieldSchema['x-component-props']
                        }
                    }
                });
                dn.refresh();
            }
        };
    }
};
const titleField = {
    name: 'titleField',
    type: 'select',
    useComponentProps () {
        var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema, _field_componentProps, _fieldSchema_xcomponentprops;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const options = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useTitleFieldOptions)();
        const { uiSchema, fieldSchema: tableColumnSchema, collectionField: tableColumnField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const targetCollectionField = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionField)();
        const collectionField = tableColumnField || targetCollectionField;
        const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
        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 == field ? void 0 : null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.fieldNames,
            ...null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['fieldNames']
        };
        return {
            title: t('Title field'),
            options,
            value: null == fieldNames ? void 0 : fieldNames.label,
            onChange (label) {
                var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema, _fieldSchema_xcomponentprops, _field_path, _field_path1;
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                const newFieldNames = {
                    ...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'] = newFieldNames;
                const target = getCollectionFields(collectionField.target).find((field)=>field.name === label);
                if ('m2o' === target.interface) fieldSchema['x-component-props']['x-next-title'] = {
                    label,
                    collection: target.collectionName
                };
                else fieldSchema['x-component-props']['x-next-title'] = null;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps.fieldNames = fieldSchema['x-component-props'].fieldNames;
                const path = null === (_field_path1 = field.path) || void 0 === _field_path1 ? void 0 : _field_path1.splice((null === (_field_path = field.path) || void 0 === _field_path ? void 0 : _field_path.length) - 1, 1);
                field.form.query("".concat(path.concat("*." + fieldSchema.name))).forEach((f)=>{
                    f.componentProps.fieldNames = fieldNames;
                });
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const allowMultiple = {
    name: 'allowMultiple',
    type: 'switch',
    useVisible () {
        const isFieldReadPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
        const collectionField = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionField)();
        return !isFieldReadPretty && [
            'hasMany',
            'belongsToMany'
        ].includes(null == collectionField ? void 0 : collectionField.type);
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { fieldSchema: tableColumnSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const { dn, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            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();
            }
        };
    }
};
const quickCreate = {
    name: 'quickCreate',
    type: 'select',
    useComponentProps () {
        var _field_componentProps;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const { dn, insertAdjacent } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            title: t('Quick create'),
            options: [
                {
                    label: t('None'),
                    value: 'none'
                },
                {
                    label: t('Dropdown'),
                    value: 'quickAdd'
                },
                {
                    label: t('Pop-up'),
                    value: 'modalAdd'
                }
            ],
            value: (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.addMode) || 'none',
            onChange (mode) {
                if ('modalAdd' === mode) {
                    const hasAddNew = fieldSchema.reduceProperties((buf, schema)=>{
                        if ('Action' === schema['x-component']) return schema;
                        return buf;
                    }, null);
                    if (!hasAddNew) {
                        const addNewActionschema = {
                            'x-action': 'create',
                            'x-acl-action': 'create',
                            title: "{{t('Add new')}}",
                            'x-toolbar': 'ActionSchemaToolbar',
                            'x-settings': 'actionSettings:addNew',
                            'x-component': 'Action',
                            'x-decorator': 'ACLActionProvider',
                            'x-component-props': {
                                openMode: 'drawer',
                                type: 'default',
                                component: 'CreateRecordAction'
                            }
                        };
                        insertAdjacent('afterBegin', addNewActionschema);
                    }
                }
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['addMode'] = mode;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps.addMode = mode;
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const setDefaultSortingRules = {
    name: 'setDefaultSortingRules',
    Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsSortingRule_mjs__.SchemaSettingsSortingRule
};
const setTheDataScope = {
    name: 'setTheDataScope',
    Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_SchemaSettingsDataScope_mjs__.SchemaSettingsDataScope,
    useComponentProps () {
        var _fieldSchema_xcomponentprops_service_params, _fieldSchema_xcomponentprops_service, _fieldSchema_xcomponentprops;
        const { getCollectionJoinField, getAllCollectionsInheritChain } = (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 record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const collectionField = tableColumnField || getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
        const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
        const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            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_service = _fieldSchema_xcomponentprops.service) || void 0 === _fieldSchema_xcomponentprops_service ? void 0 : null === (_fieldSchema_xcomponentprops_service_params = _fieldSchema_xcomponentprops_service.params) || void 0 === _fieldSchema_xcomponentprops_service_params ? void 0 : _fieldSchema_xcomponentprops_service_params.filter) || {},
            form,
            dynamicComponent: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.VariableInput, {
                    ...props,
                    form: form,
                    collectionField: props.collectionField,
                    record: record,
                    shouldChange: (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.getShouldChange)({
                        collectionField: props.collectionField,
                        variables,
                        localVariables,
                        getAllCollectionsInheritChain
                    })
                }),
            onSubmit: (param)=>{
                let { filter } = param;
                filter = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)(filter);
                __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(fieldSchema['x-component-props'], 'service.params.filter', filter);
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: fieldSchema['x-uid'],
                        'x-component-props': fieldSchema['x-component-props']
                    }
                });
            }
        };
    }
};
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { fieldSchema: tableColumnSchema, collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldSchema = tableColumnSchema || schema;
        const fieldModeOptions = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useFieldModeOptions)({
            fieldSchema: tableColumnSchema,
            collectionField
        });
        const isAddNewForm = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useIsAddNewForm)();
        const fieldMode = (0, __WEBPACK_EXTERNAL_MODULE__common_useFieldComponentName_mjs__.useFieldComponentName)();
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            title: t('Field component'),
            options: fieldModeOptions,
            value: fieldMode,
            onChange (mode) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['mode'] = mode;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps.mode = mode;
                if ((0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_util_mjs__.isSubMode)(fieldSchema) && isAddNewForm) {
                    var _field_setInitialValue, _field_setValue;
                    schema.default = null;
                    fieldSchema.default = null;
                    null == field || null === (_field_setInitialValue = field.setInitialValue) || void 0 === _field_setInitialValue || _field_setInitialValue.call(field, null);
                    null == field || null === (_field_setValue = field.setValue) || void 0 === _field_setValue || _field_setValue.call(field, null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const selectComponentFieldSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'fieldSettings:component:Select',
    items: [
        {
            ...fieldComponent,
            useVisible: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsAssociationField
        },
        {
            ...setTheDataScope,
            useVisible () {
                const isSelectFieldMode = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsSelectFieldMode)();
                const isFieldReadPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
                return isSelectFieldMode && !isFieldReadPretty;
            }
        },
        {
            ...setDefaultSortingRules,
            useComponentProps () {
                const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                return {
                    fieldSchema
                };
            },
            useVisible () {
                const isSelectFieldMode = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsSelectFieldMode)();
                const isFieldReadPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
                return isSelectFieldMode && !isFieldReadPretty;
            }
        },
        {
            ...quickCreate,
            useVisible () {
                const isAssociationField = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsAssociationField)();
                const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
                const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                return isAssociationField && !fieldSchema && !readPretty;
            }
        },
        {
            ...allowMultiple,
            useVisible () {
                const isAssociationField = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsAssociationField)();
                const IsShowMultipleSwitch = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsShowMultipleSwitch_mjs__.useIsShowMultipleSwitch)();
                return isAssociationField && IsShowMultipleSwitch();
            }
        },
        {
            ...titleField,
            useVisible: __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsAssociationField
        },
        {
            ...enableLink,
            useVisible () {
                const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
                return (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsAssociationField)() && readPretty;
            }
        },
        {
            name: 'formulatitleField',
            Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.EditFormulaTitleField,
            useVisible: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.useFormulaTitleVisible
        }
    ]
});
export { allowMultiple, selectComponentFieldSettings };
