import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
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__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__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";
const titleField = {
    name: 'titleField',
    type: 'select',
    useComponentProps () {
        var _field_componentProps, _fieldSchema_xcomponentprops, _uiSchema_xcomponentprops;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { uiSchema, fieldSchema: tableColumnSchema, collectionField: tableColumnField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
        const options = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useTitleFieldOptions)();
        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 { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const 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']) || (null == uiSchema ? void 0 : null === (_uiSchema_xcomponentprops = uiSchema['x-component-props']) || void 0 === _uiSchema_xcomponentprops ? void 0 : _uiSchema_xcomponentprops['fieldNames']);
        return {
            title: t('Title field'),
            options,
            value: null == fieldNames ? void 0 : 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
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['fieldNames'] = fieldNames;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps.fieldNames = fieldSchema['x-component-props'].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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        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 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 isAddNewForm = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useIsAddNewForm)();
        const fieldComponentName = (0, __WEBPACK_EXTERNAL_MODULE__common_useFieldComponentName_mjs__.useFieldComponentName)();
        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 { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            title: t('Field component'),
            options: fieldModeOptions,
            value: fieldComponentName,
            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 recordPickerComponentFieldSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'fieldSettings:component:Picker',
    items: [
        fieldComponent,
        {
            name: 'popupSize',
            type: 'select',
            useVisible () {
                const isFieldReadPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_FormItem_Settings_mjs__.useIsFieldReadPretty)();
                return !isFieldReadPretty;
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
                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)();
                var _fieldSchema_xcomponentprops_openSize;
                return {
                    title: t('Popup size'),
                    options: [
                        {
                            label: t('Small'),
                            value: 'small'
                        },
                        {
                            label: t('Middle'),
                            value: 'middle'
                        },
                        {
                            label: t('Large'),
                            value: 'large'
                        }
                    ],
                    value: null !== (_fieldSchema_xcomponentprops_openSize = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['openSize']) && void 0 !== _fieldSchema_xcomponentprops_openSize ? _fieldSchema_xcomponentprops_openSize : (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1['openMode']) == 'modal' ? 'large' : 'middle',
                    onChange: (value)=>{
                        field.componentProps.openSize = value;
                        fieldSchema['x-component-props'] = {
                            ...fieldSchema['x-component-props'],
                            openSize: value
                        };
                        dn.emit('patch', {
                            schema: {
                                'x-uid': fieldSchema['x-uid'],
                                'x-component-props': fieldSchema['x-component-props']
                            }
                        });
                        dn.refresh();
                    }
                };
            }
        },
        allowMultiple,
        titleField
    ]
});
export { recordPickerComponentFieldSettings, titleField };
