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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FilterItems_mjs__ from "./FilterItems.mjs";
const FilterGroup = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { bordered = true, disabled } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const remove = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.RemoveConditionContext);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.useToken)();
    const keys = Object.keys(field.value || {});
    const logic = keys.includes('$or') ? '$or' : '$and';
    const setLogic = (value)=>{
        const obj = field.value || {};
        field.value = {
            [value]: [
                ...obj[logic] || []
            ]
        };
    };
    const mergedDisabled = disabled || field.disabled;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.FilterLogicContext.Provider, {
        value: logic,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            style: bordered ? {
                position: 'relative',
                border: "1px dashed ".concat(token.colorBorder),
                padding: token.paddingSM,
                marginBottom: token.marginXS
            } : {
                position: 'relative',
                marginBottom: token.marginXS
            },
            children: [
                remove && !mergedDisabled && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                    role: "button",
                    "aria-label": "icon-close",
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CloseCircleOutlined, {
                        style: {
                            position: 'absolute',
                            right: 10,
                            top: 10,
                            color: '#bfbfbf'
                        },
                        onClick: ()=>remove()
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    style: {
                        marginBottom: 8,
                        color: token.colorText
                    },
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_i18next__.Trans, {
                        children: [
                            'Meet ',
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
                                role: "button",
                                "data-testid": "filter-select-all-or-any",
                                style: {
                                    width: 'auto'
                                },
                                value: logic,
                                onChange: (value)=>{
                                    setLogic(value);
                                },
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select.Option, {
                                        value: '$and',
                                        children: "All"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select.Option, {
                                        value: '$or',
                                        children: "Any"
                                    })
                                ]
                            }),
                            ' conditions in the group'
                        ]
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.ArrayFieldComponent, {
                        name: logic,
                        component: [
                            __WEBPACK_EXTERNAL_MODULE__FilterItems_mjs__.FilterItems
                        ],
                        disabled: mergedDisabled
                    })
                }),
                !mergedDisabled && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                    size: 16,
                    style: {
                        marginTop: 8,
                        marginBottom: 8
                    },
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                            onClick: ()=>{
                                const value = field.value || {};
                                const items = value[logic] || [];
                                items.push({});
                                field.value = {
                                    [logic]: items
                                };
                            },
                            children: t('Add condition')
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                            onClick: ()=>{
                                const value = field.value || {};
                                const items = value[logic] || [];
                                items.push({
                                    $and: [
                                        {}
                                    ]
                                });
                                field.value = {
                                    [logic]: items
                                };
                            },
                            children: t('Add condition group')
                        })
                    ]
                })
            ]
        })
    });
});
export { FilterGroup };
