"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    subTablePopoverComponentFieldSettings: ()=>subTablePopoverComponentFieldSettings,
    allowAddNewData: ()=>allowAddNewData,
    clickToSelect: ()=>clickToSelect,
    setDefaultSortingRules: ()=>setDefaultSortingRules
});
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_react_i18next_namespaceObject = require("react-i18next");
const SchemaSettings_js_namespaceObject = require("../../../../application/schema-settings/SchemaSettings.js");
const index_js_namespaceObject = require("../../../../collection-manager/index.js");
const useFieldComponentName_js_namespaceObject = require("../../../../common/useFieldComponentName.js");
const external_data_source_index_js_namespaceObject = require("../../../../data-source/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../../schema-component/index.js");
const util_js_namespaceObject = require("../../../../schema-component/antd/association-field/util.js");
const form_item_index_js_namespaceObject = require("../../../../schema-component/antd/form-item/index.js");
const external_secondLevelSelect_js_namespaceObject = require("./secondLevelSelect.js");
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
        const field = (0, schema_namespaceObject.useField)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const fieldModeOptions = (0, external_schema_component_index_js_namespaceObject.useFieldModeOptions)();
        const isAddNewForm = (0, external_schema_component_index_js_namespaceObject.useIsAddNewForm)();
        const fieldComponentName = (0, useFieldComponentName_js_namespaceObject.useFieldComponentName)();
        const { dn } = (0, external_schema_component_index_js_namespaceObject.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, util_js_namespaceObject.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, form_item_index_js_namespaceObject.useIsFieldReadPretty)();
        return !readPretty;
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
        const field = (0, schema_namespaceObject.useField)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const { dn, refresh } = (0, external_schema_component_index_js_namespaceObject.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, index_js_namespaceObject.useCollectionManager_deprecated)();
        const field = (0, schema_namespaceObject.useField)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const association = fieldSchema['x-collection-field'];
        const { target } = getCollectionJoinField(association) || {};
        const sortFields = (0, index_js_namespaceObject.useSortFields)(target);
        const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
        const { dn } = (0, external_schema_component_index_js_namespaceObject.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: components_namespaceObject.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
                                                },
                                                optionLabelProp: 'fullLabel'
                                            }
                                        },
                                        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, index_js_namespaceObject.useCollectionManager_deprecated)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const association = fieldSchema['x-collection-field'];
        const { interface: targetInterface } = getCollectionJoinField(association) || {};
        const readPretty = (0, form_item_index_js_namespaceObject.useIsFieldReadPretty)();
        return readPretty && [
            'm2m',
            'o2m'
        ].includes(targetInterface);
    }
};
const allowAddNewData = {
    name: 'allowAddNewData',
    type: 'switch',
    useVisible () {
        const readPretty = (0, form_item_index_js_namespaceObject.useIsFieldReadPretty)();
        const isAssociationField = (0, form_item_index_js_namespaceObject.useIsAssociationField)();
        return !readPretty && isAssociationField;
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
        const field = (0, schema_namespaceObject.useField)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const { dn, refresh } = (0, external_schema_component_index_js_namespaceObject.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 clickToSelect = {
    name: 'clicktoselect',
    type: 'modal',
    useVisible () {
        const readPretty = (0, form_item_index_js_namespaceObject.useIsFieldReadPretty)();
        const isAssociationField = (0, form_item_index_js_namespaceObject.useIsAssociationField)();
        return !readPretty && isAssociationField;
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops_quickAddField, _fieldSchema_xcomponentprops_quickAddParentCollection, _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
        const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
        const field = (0, schema_namespaceObject.useField)();
        const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
        const { dn, refresh } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
        const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
        const cm = (0, external_data_source_index_js_namespaceObject.useCollectionManager)();
        const [firstLevelValue, setFirstLevelValue] = (0, external_react_namespaceObject.useState)((null === (_fieldSchema_xcomponentprops_quickAddField = fieldSchema['x-component-props']['quickAddField']) || void 0 === _fieldSchema_xcomponentprops_quickAddField ? void 0 : _fieldSchema_xcomponentprops_quickAddField.value) || 'none');
        const options = cm.getCollection(fieldSchema['x-collection-field']);
        const isAddNewForm = (0, external_schema_component_index_js_namespaceObject.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'
        });
        const Parentoptions = cm.getCollectionFields("".concat(fieldSchema['x-collection-field'], ".").concat(firstLevelValue));
        const fieldParentTabsOptions = Parentoptions.map((item)=>{
            if ('m2o' === item.interface) return {
                ...item,
                label: compile(item.uiSchema.title),
                value: item.name
            };
        }).filter(Boolean);
        fieldParentTabsOptions.unshift({
            label: t('none'),
            value: 'none'
        });
        const defParentVal = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_quickAddParentCollection = _fieldSchema_xcomponentprops['quickAddParentCollection']) || void 0 === _fieldSchema_xcomponentprops_quickAddParentCollection ? void 0 : _fieldSchema_xcomponentprops_quickAddParentCollection.value) || 'none';
        const schema = {
            type: 'object',
            title: t('Click to select'),
            properties: {
                isquickaddtabs: {
                    title: t('Enable quick create'),
                    type: 'boolean',
                    default: (null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1.isQuickAdd) || false,
                    'x-decorator': 'FormItem',
                    'x-component': 'Switch',
                    'x-component-props': {}
                },
                firstLevelselection: {
                    title: t('Set Quick Add Tabs'),
                    default: firstLevelValue,
                    'x-decorator': 'FormItem',
                    'x-component': 'Select',
                    enum: fieldTabsOptions,
                    'x-reactions': [
                        {
                            dependencies: [
                                'isquickaddtabs'
                            ],
                            fulfill: {
                                state: {
                                    visible: '{{$deps[0] === true}}'
                                }
                            }
                        }
                    ],
                    'x-component-props': {
                        onChange (value) {
                            setFirstLevelValue(value);
                        }
                    }
                },
                secondLevelselection: {
                    title: t('Set Quick Add Parent Tabs'),
                    default: defParentVal,
                    'x-decorator': 'FormItem',
                    'x-component': 'SecondLevelSelect',
                    'x-reactions': [
                        {
                            dependencies: [
                                'firstLevelselection',
                                'isquickaddtabs'
                            ],
                            fulfill: {
                                state: {
                                    visible: '{{$deps[0] !== "none" && $deps[1] === true}}'
                                },
                                schema: {
                                    'x-component-props': {
                                        firstLevelValue: '{{$deps[0]}}',
                                        collectionField: fieldSchema['x-collection-field']
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        };
        return {
            components: {
                Switch: components_namespaceObject.Switch,
                SecondLevelSelect: external_secondLevelSelect_js_namespaceObject.SecondLevelSelect
            },
            title: t('Quick create'),
            schema,
            onSubmit: async (param)=>{
                let { isquickaddtabs, firstLevelselection, secondLevelselection } = param;
                var _fieldTabsOptions_find;
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                field.componentProps['isQuickAdd'] = isquickaddtabs;
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['isQuickAdd'] = isquickaddtabs;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['quickAddField'] = {
                    fieldInterface: (null === (_fieldTabsOptions_find = fieldTabsOptions.find((item)=>item.value === firstLevelselection)) || void 0 === _fieldTabsOptions_find ? void 0 : _fieldTabsOptions_find['interface']) || 'none',
                    value: firstLevelselection
                };
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps['quickAddField'] = fieldSchema['x-component-props']['quickAddField'];
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['quickAddParentCollection'] = {
                    collectionField: "".concat(fieldSchema['x-collection-field'], ".").concat(firstLevelselection, ".").concat(secondLevelselection),
                    value: secondLevelselection
                };
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps['quickAddParentCollection'] = {
                    collectionField: "".concat(fieldSchema['x-collection-field'], ".").concat(firstLevelselection, ".").concat(secondLevelselection),
                    value: secondLevelselection
                };
                if ((0, util_js_namespaceObject.isSubMode)(fieldSchema) && isAddNewForm) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema: schema
                });
                refresh();
                dn.refresh();
            }
        };
    }
};
const subTablePopoverComponentFieldSettings = new SchemaSettings_js_namespaceObject.SchemaSettings({
    name: 'fieldSettings:component:SubTable',
    items: [
        fieldComponent,
        allowAddNewData,
        allowSelectExistingRecord,
        setDefaultSortingRules,
        clickToSelect
    ]
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
