import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__ from "../../built-in/acl/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__ from "../../i18n/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_index_mjs__ from "../../style/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__ from "../../style/theme/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { scope, components } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponentOptions, {
        scope: {
            ...scope
        },
        components: {
            ArrayItems: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayItems,
            FormItem: __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.FormItem,
            Space: __WEBPACK_EXTERNAL_MODULE_antd__.Space
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
            schema: schema,
            ...props
        })
    });
}, {
    displayName: 'FieldComponentProps'
});
const useFieldComponents = ()=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.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, __WEBPACK_EXTERNAL_MODULE_react__.memo)((props)=>{
    const { locale } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider.ConfigContext);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { scope, components } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaOptionsContext);
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__.useGlobalTheme)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { insert } = props;
    const itemConfig = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useSchemaInitializerItem)();
    const { getInterface } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { collections, getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const allCollection = collections.map((value)=>({
            label: value.title,
            value: value.name
        }));
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionManager)();
    const { options: fieldComponents, values: fieldComponentValues } = useFieldComponents();
    (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const handleClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async ()=>{
        var _getInterface_default, _getInterface, _cm_getCollection;
        const values = await (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.FormDialog)(t('Add custom field'), ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponentOptions, {
                scope: {
                    ...scope,
                    useCollectionManager_deprecated: __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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: __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.Select
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                    layout: 'vertical',
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider, {
                        locale: locale,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)())
            },
            effects () {
                (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.gridRowColWrap)(schema));
    }, [
        theme
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerItem, {
        ...itemConfig,
        ...props,
        onClick: handleClick
    });
});
const FilterFormItemCustom = ()=>{
    const { insertAdjacent } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FilterCustomItemInitializer, {
        insert: (s)=>insertAdjacent('beforeEnd', s)
    });
};
const FilterItemCustomDesigner = ()=>{
    var _fieldSchema_xcomponentprops_params, _fieldSchema_xcomponentprops;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const fieldName = fieldSchema['name'];
    const name = fieldName.includes('__custom') ? fieldSchema['collectionName'] : fieldName;
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const component = fieldSchema['x-component'];
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.EditTitle, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.EditDescription, {}),
            'Input' !== component ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.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;
                    __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingCollection, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingComponent, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.EditTitleField, {}) : null,
            'Select' === component || 'AutoComplete' === component ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.EditFormulaTitleField, {}) : null,
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.EditCustomDefaultValue, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsRemove, {})
        ]
    });
};
const useCollectionJoinFieldTitle = (name)=>{
    const { getCollection, getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.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 = __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(null == collection ? void 0 : collection.title, {
            t: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.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(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(fieldTitle, {
                t: __WEBPACK_EXTERNAL_MODULE__i18n_index_mjs__.i18n.t
            }));
            cName = (null == field ? void 0 : field.target) ? field.target : null;
        }
        return title;
    }, [
        name
    ]);
};
const FilterFormItem = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _schema_xdecoratorprops;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>'string' == typeof field.description ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            dangerouslySetInnerHTML: {
                __html: (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.HTMLEncode)(field.description).split('\n').join('<br/>')
            }
        }) : field.description, [
        field.description
    ]);
    const className = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__style_index_mjs__.cx)((0, __WEBPACK_EXTERNAL_MODULE__style_index_mjs__.css)(_templateObject()), {
            [(0, __WEBPACK_EXTERNAL_MODULE__style_index_mjs__.css)(_templateObject1())]: false === showTitle
        }), [
        showTitle
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__.ACLCollectionFieldProvider, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.BlockItem, {
            className: 'tb-form-item',
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.FormItem, {
                className: className,
                ...props,
                extra: extra
            })
        })
    });
}, {
    displayName: 'FilterFormItem'
});
export { FilterCustomItemInitializer, FilterFormItem, FilterFormItemCustom, FilterItemCustomDesigner, useCollectionJoinFieldTitle, useFieldComponents };
