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_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__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__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_index_mjs__ from "../../../../schema-component/antd/form-item/index.mjs";
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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const fieldModeOptions = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useFieldModeOptions)();
        const isAddNewForm = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useIsAddNewForm)();
        const fieldComponentName = (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: 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) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const allowSelectExistingRecord = {
    name: 'allowSelectExistingRecord',
    type: 'switch',
    useVisible () {
        const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsFieldReadPretty)();
        return !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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const { dn, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        return {
            title: t('Allow selection of existing records'),
            checked: null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.allowSelectExistingRecord,
            onChange (value) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                field.componentProps.allowSelectExistingRecord = value;
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props'].allowSelectExistingRecord = value;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                dn.emit('patch', {
                    schema
                });
                refresh();
            }
        };
    }
};
const setDefaultSortingRules = {
    name: 'SetDefaultSortingRules',
    type: 'modal',
    useComponentProps () {
        const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const association = fieldSchema['x-collection-field'];
        const { target } = getCollectionJoinField(association) || {};
        const sortFields = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useSortFields)(target);
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const defaultSort = field.componentProps.sortArr || [];
        const sort = null == defaultSort ? void 0 : defaultSort.map((item)=>(null == item ? void 0 : item.startsWith('-')) ? {
                field: item.substring(1),
                direction: 'desc'
            } : {
                field: item,
                direction: 'asc'
            });
        return {
            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,
                                            required: true,
                                            '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);
                field.componentProps.sortArr = sortArr;
                fieldSchema['x-component-props'].sortArr = sortArr;
                dn.emit('patch', {
                    schema: {
                        ['x-uid']: fieldSchema['x-uid'],
                        'x-component-props': fieldSchema['x-component-props']
                    }
                });
            }
        };
    },
    useVisible () {
        const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const association = fieldSchema['x-collection-field'];
        const { interface: targetInterface } = getCollectionJoinField(association) || {};
        const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsFieldReadPretty)();
        return readPretty && [
            'm2m',
            'o2m'
        ].includes(targetInterface);
    }
};
const allowAddNewData = {
    name: 'allowAddNewData',
    type: 'switch',
    useVisible () {
        const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsFieldReadPretty)();
        const isAssociationField = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsAssociationField)();
        return !readPretty && isAssociationField;
    },
    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 add new'),
            checked: (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.allowAddnew) !== false,
            onChange (value) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                field.componentProps.allowAddnew = value;
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props'].allowAddnew = value;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                dn.emit('patch', {
                    schema
                });
                refresh();
            }
        };
    }
};
const isQuickAddTabs = {
    name: 'isquickaddtabs',
    type: 'switch',
    useVisible () {
        const readPretty = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsFieldReadPretty)();
        const isAssociationField = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_index_mjs__.useIsAssociationField)();
        return !readPretty && isAssociationField;
    },
    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('Is Quick Add Tabs'),
            checked: (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.isQuickAdd) || false,
            onChange (value) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                field.componentProps['isQuickAdd'] = value;
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['isQuickAdd'] = value;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                dn.emit('patch', {
                    schema
                });
                refresh();
            }
        };
    }
};
const setQuickAddTabs = {
    name: 'setquickaddtabs',
    type: 'select',
    useComponentProps () {
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionManager)();
        const options = cm.getCollection(fieldSchema['x-collection-field']);
        const defVal = fieldSchema['x-component-props']['quickAddField'] || 'none';
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const isAddNewForm = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useIsAddNewForm)();
        const fieldTabsOptions = options.fields.map((item)=>{
            if ('m2o' === item.interface || 'select' === item.interface) {
                var _item_uiSchema;
                return {
                    ...item,
                    label: compile(null === (_item_uiSchema = item.uiSchema) || void 0 === _item_uiSchema ? void 0 : _item_uiSchema.title),
                    value: item.name
                };
            }
        }).filter(Boolean);
        fieldTabsOptions.unshift({
            label: 'none',
            value: 'none'
        });
        return {
            title: t('Set Quick Add Tabs'),
            options: fieldTabsOptions,
            value: defVal,
            onChange (mode) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['quickAddField'] = {
                    fieldInterface: fieldTabsOptions.find((item)=>item.value === mode)['interface'] || 'none',
                    value: mode
                };
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps['quickAddField'] = fieldSchema['x-component-props']['quickAddField'];
                if ((0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_util_mjs__.isSubMode)(fieldSchema) && isAddNewForm) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    },
    useVisible () {
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        return fieldSchema['x-component-props']['isQuickAdd'];
    }
};
const setQuickAddParentTabs = {
    name: 'setquickaddparenttabs',
    type: 'select',
    useComponentProps () {
        var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops_quickAddParentCollection, _fieldSchema_xcomponentprops1;
        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionManager)();
        const { value: quickField } = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['quickAddField']) || {};
        const options = cm.getCollectionFields("".concat(fieldSchema['x-collection-field'], ".").concat(quickField));
        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const isAddNewForm = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useIsAddNewForm)();
        const fieldTabsOptions = options.map((item)=>{
            if ('m2o' === item.interface) return {
                ...item,
                label: compile(item.uiSchema.title),
                value: item.name
            };
        }).filter(Boolean);
        fieldTabsOptions.unshift({
            label: t('none'),
            value: 'none'
        });
        const defValue = (null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : null === (_fieldSchema_xcomponentprops_quickAddParentCollection = _fieldSchema_xcomponentprops1['quickAddParentCollection']) || void 0 === _fieldSchema_xcomponentprops_quickAddParentCollection ? void 0 : _fieldSchema_xcomponentprops_quickAddParentCollection.value) || 'none';
        return {
            title: t('Set Quick Add Parent Tabs'),
            options: fieldTabsOptions,
            value: defValue,
            onChange (mode) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['quickAddParentCollection'] = {
                    collectionField: "".concat(fieldSchema['x-collection-field'], ".").concat(quickField, ".").concat(mode),
                    value: mode
                };
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps['quickAddParentCollection'] = {
                    collectionField: "".concat(fieldSchema['x-collection-field'], ".").concat(quickField, ".").concat(mode),
                    value: mode
                };
                if ((0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_util_mjs__.isSubMode)(fieldSchema) && isAddNewForm) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    },
    useVisible () {
        const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
        return fieldSchema['x-component-props']['isQuickAdd'] && fieldSchema['x-component-props']['quickAddField'] && 'none' !== fieldSchema['x-component-props']['quickAddField'];
    }
};
const subTablePopoverComponentFieldSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'fieldSettings:component:SubTable',
    items: [
        fieldComponent,
        allowAddNewData,
        allowSelectExistingRecord,
        setDefaultSortingRules,
        isQuickAddTabs,
        setQuickAddTabs,
        setQuickAddParentTabs
    ]
});
export { allowAddNewData, isQuickAddTabs, setDefaultSortingRules, setQuickAddParentTabs, setQuickAddTabs, subTablePopoverComponentFieldSettings };
