"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    FilterFormItem: ()=>FilterFormItem,
    FilterFormItemCustom: ()=>FilterFormItemCustom,
    FilterItemCustomDesigner: ()=>FilterItemCustomDesigner,
    FilterCustomItemInitializer: ()=>FilterCustomItemInitializer,
    useCollectionJoinFieldTitle: ()=>useCollectionJoinFieldTitle,
    useFieldComponents: ()=>useFieldComponents
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_application_index_js_namespaceObject = require("../../application/index.js");
const external_block_provider_index_js_namespaceObject = require("../../block-provider/index.js");
const acl_index_js_namespaceObject = require("../../built-in/acl/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../collection-manager/index.js");
const external_data_source_index_js_namespaceObject = require("../../data-source/index.js");
const external_i18n_index_js_namespaceObject = require("../../i18n/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_schema_settings_index_js_namespaceObject = require("../../schema-settings/index.js");
const external_style_index_js_namespaceObject = require("../../style/index.js");
const theme_index_js_namespaceObject = require("../../style/theme/index.js");
const external_utils_js_namespaceObject = require("../utils.js");
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n          & .ant-space {\n            flex-wrap: wrap;\n          }\n        "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n            > .ant-formily-item-label {\n              display: none;\n            }\n          "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const FieldComponentProps = (0, schema_namespaceObject.observer)((props)=>{
    const { scope, components } = (0, external_react_namespaceObject.useContext)(schema_namespaceObject.SchemaOptionsContext);
    const schema = {
        type: 'object',
        properties: {
            options: {
                title: '{{t("Options")}}',
                type: 'array',
                'x-decorator': 'FormItem',
                'x-component': 'ArrayItems',
                items: {
                    type: 'object',
                    properties: {
                        space: {
                            type: 'void',
                            'x-component': 'Space',
                            properties: {
                                sort: {
                                    type: 'void',
                                    'x-decorator': 'FormItem',
                                    'x-component': 'ArrayItems.SortHandle'
                                },
                                label: {
                                    type: 'string',
                                    'x-decorator': 'FormItem',
                                    'x-component': 'Input',
                                    'x-component-props': {
                                        placeholder: '{{t("Option label")}}'
                                    },
                                    required: true
                                },
                                value: {
                                    type: 'string',
                                    'x-decorator': 'FormItem',
                                    'x-component': 'Input',
                                    'x-component-props': {
                                        placeholder: '{{t("Option value")}}'
                                    },
                                    required: true
                                },
                                remove: {
                                    type: 'void',
                                    'x-decorator': 'FormItem',
                                    'x-component': 'ArrayItems.Remove'
                                }
                            }
                        }
                    }
                },
                properties: {
                    add: {
                        type: 'void',
                        title: '{{t("add")}}',
                        'x-component': 'ArrayItems.Addition'
                    }
                }
            }
        }
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponentOptions, {
        scope: {
            ...scope
        },
        components: {
            ArrayItems: components_namespaceObject.ArrayItems,
            FormItem: external_schema_component_index_js_namespaceObject.FormItem,
            Space: external_antd_namespaceObject.Space
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
            schema: schema,
            ...props
        })
    });
}, {
    displayName: 'FieldComponentProps'
});
const useFieldComponents = ()=>{
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const options = [
        {
            label: t('Input'),
            value: 'Input'
        },
        {
            label: t('AutoComplete'),
            value: 'AutoComplete'
        },
        {
            label: t('Select'),
            value: 'Select'
        },
        {
            label: t('AssociationCascader'),
            value: 'AssociationCascader'
        },
        {
            label: t('DatePicker'),
            value: 'DatePicker'
        },
        {
            label: t('Radio group'),
            value: 'Radio.Group'
        },
        {
            label: t('Checkbox group'),
            value: 'Checkbox.Group'
        }
    ];
    return {
        options,
        values: options.map((option)=>option.value)
    };
};
const FilterCustomItemInitializer = /*#__PURE__*/ (0, external_react_namespaceObject.memo)((props)=>{
    const { locale } = (0, external_react_namespaceObject.useContext)(external_antd_namespaceObject.ConfigProvider.ConfigContext);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { scope, components } = (0, external_react_namespaceObject.useContext)(schema_namespaceObject.SchemaOptionsContext);
    const { theme } = (0, theme_index_js_namespaceObject.useGlobalTheme)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { insert } = props;
    const itemConfig = (0, external_application_index_js_namespaceObject.useSchemaInitializerItem)();
    const { getInterface } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const { collections, getCollectionFields } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const allCollection = collections.map((value)=>({
            label: value.title,
            value: value.name
        }));
    const cm = (0, external_data_source_index_js_namespaceObject.useCollectionManager)();
    const { options: fieldComponents, values: fieldComponentValues } = useFieldComponents();
    (0, index_js_namespaceObject.useAPIClient)();
    const handleClick = (0, external_react_namespaceObject.useCallback)(async ()=>{
        var _getInterface_default, _getInterface, _cm_getCollection;
        const values = await (0, external_schema_component_index_js_namespaceObject.FormDialog)(t('Add custom field'), ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponentOptions, {
                scope: {
                    ...scope,
                    useCollectionManager_deprecated: external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated,
                    useAssociationFields (collection) {
                        var _getCollectionFields;
                        var _getCollectionFields_map;
                        return null !== (_getCollectionFields_map = null === (_getCollectionFields = getCollectionFields(collection)) || void 0 === _getCollectionFields ? void 0 : _getCollectionFields.map((field)=>{
                            var _field_uiSchema;
                            return {
                                value: field.name,
                                label: compile(null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title)
                            };
                        })) && void 0 !== _getCollectionFields_map ? _getCollectionFields_map : [];
                    }
                },
                components: {
                    ...components,
                    FieldComponentProps,
                    Select: external_schema_component_index_js_namespaceObject.Select
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormLayout, {
                    layout: 'vertical',
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.ConfigProvider, {
                        locale: locale,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
                            schema: {
                                properties: {
                                    name: {
                                        type: 'string',
                                        required: true
                                    },
                                    title: {
                                        type: 'string',
                                        title: t('Field title'),
                                        'x-component': 'Input',
                                        'x-decorator': 'FormItem',
                                        required: true
                                    },
                                    component: {
                                        type: 'string',
                                        title: t('Field component'),
                                        'x-component': 'Select',
                                        'x-decorator': 'FormItem',
                                        required: true,
                                        enum: fieldComponents
                                    },
                                    collection: {
                                        type: 'string',
                                        title: t('Field collection'),
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Select',
                                        enum: allCollection,
                                        description: t('Select a collection field to use metadata of the field'),
                                        'x-visible': false
                                    },
                                    associationField: {
                                        type: 'string',
                                        title: t('Parent Association field'),
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Select',
                                        'x-visible': false,
                                        required: true,
                                        'x-reactions': [
                                            {
                                                dependencies: [
                                                    'collection',
                                                    'component'
                                                ],
                                                fulfill: {
                                                    schema: {
                                                        'x-visible': "{{$deps[0] && $deps[1] === 'AssociationCascader'}}",
                                                        enum: '{{ useAssociationFields($deps[0]) }}'
                                                    }
                                                }
                                            }
                                        ]
                                    },
                                    props: {
                                        type: 'object',
                                        title: t('Component properties'),
                                        'x-decorator': 'FormItem',
                                        'x-component': 'FieldComponentProps',
                                        'x-visible': false,
                                        'x-reactions': [
                                            {
                                                dependencies: [
                                                    'component'
                                                ],
                                                fulfill: {
                                                    schema: {
                                                        'x-visible': "{{$deps[0] === 'Radio.Group' || $deps[0]==='Checkbox.Group'}}"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        })
                    })
                })
            }), theme).open({
            values: {
                name: "f_".concat((0, schema_namespaceObject.uid)())
            },
            effects () {
                (0, schema_namespaceObject.onFieldValueChange)('component', (field)=>{
                    const name = field.value;
                    const collectionComponent = field.query('.collection').take();
                    const propsComponent = field.query('.props').take();
                    if ('Select' === name || 'AutoComplete' === name || 'AssociationCascader' === name) {
                        collectionComponent.setDisplay('visible');
                        collectionComponent.setRequired(true);
                        propsComponent.setDisplay('none');
                        propsComponent.setRequired(false);
                    } else if ('CustomSelect' === name) {
                        collectionComponent.setDisplay('none');
                        collectionComponent.setRequired(false);
                        propsComponent.setDisplay('visible');
                        propsComponent.setRequired(true);
                    } else {
                        collectionComponent.setDisplay('none');
                        collectionComponent.setRequired(false);
                        propsComponent.setDisplay('none');
                        propsComponent.setRequired(false);
                    }
                });
            }
        });
        const { title, component, collection, associationField, props } = values;
        const defaultSchema = (null === (_getInterface = getInterface(component)) || void 0 === _getInterface ? void 0 : null === (_getInterface_default = _getInterface.default) || void 0 === _getInterface_default ? void 0 : _getInterface_default.uiSchema) || {};
        const titleField = null === (_cm_getCollection = cm.getCollection(collection)) || void 0 === _cm_getCollection ? void 0 : _cm_getCollection.titleField;
        const name = (0, schema_namespaceObject.uid)();
        const schema = {
            ...defaultSchema,
            type: 'string',
            title: title,
            name: '__custom.' + name,
            required: false,
            'x-component': component,
            'x-toolbar': 'FormItemSchemaToolbar',
            'x-settings': 'fieldSettings:FilterFormCustomSettings',
            'x-decorator': 'FilterFormItem',
            'x-decorator-props': collection,
            'x-component-props': {
                ...defaultSchema['x-component-props'] || {},
                fieldNames: {
                    label: titleField,
                    value: titleField
                },
                associationField,
                collection,
                objectValue: true,
                component: component,
                ...props
            },
            collectionName: collection
        };
        if ('DatePicker' === component) {
            schema['x-settings'] = 'fieldSettings:FilterFormItem';
            schema['x-designer-props'] = {
                interface: 'datetime'
            };
        }
        insert((0, external_utils_js_namespaceObject.gridRowColWrap)(schema));
    }, [
        theme
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_application_index_js_namespaceObject.SchemaInitializerItem, {
        ...itemConfig,
        ...props,
        onClick: handleClick
    });
});
const FilterFormItemCustom = ()=>{
    const { insertAdjacent } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FilterCustomItemInitializer, {
        insert: (s)=>insertAdjacent('beforeEnd', s)
    });
};
const FilterItemCustomDesigner = ()=>{
    var _fieldSchema_xcomponentprops_params, _fieldSchema_xcomponentprops;
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const fieldName = fieldSchema['name'];
    const name = fieldName.includes('__custom') ? fieldSchema['collectionName'] : fieldName;
    const { form } = (0, external_block_provider_index_js_namespaceObject.useFormBlockContext)();
    const field = (0, schema_namespaceObject.useField)();
    const { dn } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    const component = fieldSchema['x-component'];
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_settings_index_js_namespaceObject.GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.EditTitle, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.EditDescription, {}),
            'Input' !== component ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsDataScope, {
                collectionName: name,
                defaultFilter: (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_params = _fieldSchema_xcomponentprops.params) || void 0 === _fieldSchema_xcomponentprops_params ? void 0 : _fieldSchema_xcomponentprops_params.filter) || {},
                form: form,
                onSubmit: (param)=>{
                    let { filter } = param;
                    var _field_componentProps;
                    external_lodash_default().set(field.componentProps, 'params', {
                        ...null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.params,
                        filter
                    });
                    fieldSchema['x-component-props']['params'] = field.componentProps.params;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            'x-component-props': fieldSchema['x-component-props']
                        }
                    });
                }
            }) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingCollection, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingComponent, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.EditTitleField, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.EditFormulaTitleField, {}) : null,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.EditCustomDefaultValue, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsRemove, {})
        ]
    });
};
const useCollectionJoinFieldTitle = (name)=>{
    const { getCollection, getCollectionField } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    return (0, external_react_namespaceObject.useMemo)(()=>{
        if (!name) return;
        const [collectionName, ...fieldNames] = name.split('.');
        if (!(null == fieldNames ? void 0 : fieldNames.length)) return;
        const collection = getCollection(collectionName);
        let cName = collectionName;
        let field;
        let title = schema_namespaceObject.Schema.compile(null == collection ? void 0 : collection.title, {
            t: external_i18n_index_js_namespaceObject.i18n.t
        });
        while(cName && fieldNames.length > 0){
            var _field_uiSchema;
            const fileName = fieldNames.shift();
            field = getCollectionField("".concat(cName, ".").concat(fileName));
            const fieldTitle = (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 (fieldTitle) title += " / ".concat(schema_namespaceObject.Schema.compile(fieldTitle, {
                t: external_i18n_index_js_namespaceObject.i18n.t
            }));
            cName = (null == field ? void 0 : field.target) ? field.target : null;
        }
        return title;
    }, [
        name
    ]);
};
const FilterFormItem = (0, schema_namespaceObject.observer)((props)=>{
    var _schema_xdecoratorprops;
    const field = (0, schema_namespaceObject.useField)();
    const schema = (0, schema_namespaceObject.useFieldSchema)();
    var _schema_xdecoratorprops_showTitle;
    const showTitle = null === (_schema_xdecoratorprops_showTitle = null === (_schema_xdecoratorprops = schema['x-decorator-props']) || void 0 === _schema_xdecoratorprops ? void 0 : _schema_xdecoratorprops.showTitle) || void 0 === _schema_xdecoratorprops_showTitle || _schema_xdecoratorprops_showTitle;
    const extra = (0, external_react_namespaceObject.useMemo)(()=>'string' == typeof field.description ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            dangerouslySetInnerHTML: {
                __html: (0, external_schema_component_index_js_namespaceObject.HTMLEncode)(field.description).split('\n').join('<br/>')
            }
        }) : field.description, [
        field.description
    ]);
    const className = (0, external_react_namespaceObject.useMemo)(()=>(0, external_style_index_js_namespaceObject.cx)((0, external_style_index_js_namespaceObject.css)(_templateObject()), {
            [(0, external_style_index_js_namespaceObject.css)(_templateObject1())]: false === showTitle
        }), [
        showTitle
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(acl_index_js_namespaceObject.ACLCollectionFieldProvider, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.BlockItem, {
            className: 'tb-form-item',
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.FormItem, {
                className: className,
                ...props,
                extra: extra
            })
        })
    });
}, {
    displayName: 'FilterFormItem'
});
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
});
