import { jsx, jsxs } from "react/jsx-runtime";
import { useCallback, useContext, useMemo } from "react";
import { observer } from "@tachybase/schema";
import { CloseCircleOutlined } from "@ant-design/icons";
import { Cascader, Select, Space } from "antd";
import { useTranslation } from "react-i18next";
import { useCompile } from "../../index.mjs";
import { RemoveConditionContext } from "./context.mjs";
import { DynamicComponent } from "./DynamicComponent.mjs";
import { useValues } from "./useValues.mjs";
const FilterItem = observer((props)=>{
    const { t } = useTranslation();
    const compile = useCompile();
    const remove = useContext(RemoveConditionContext);
    const { schema, fields, operators, dataIndex, operator, setDataIndex, setOperator, value, setValue, collectionField } = useValues();
    const style = useMemo(()=>({
            marginBottom: 8
        }), []);
    const fieldNames = useMemo(()=>({
            label: 'title',
            value: 'name',
            children: 'children'
        }), []);
    const onChange = useCallback((value)=>{
        setDataIndex(value);
    }, [
        setDataIndex
    ]);
    const onOperatorsChange = useCallback((value)=>{
        setOperator(value);
    }, [
        setOperator
    ]);
    const removeStyle = useMemo(()=>({
            color: '#bfbfbf'
        }), []);
    return /*#__PURE__*/ jsx("div", {
        style: style,
        className: "nc-filter-item",
        children: /*#__PURE__*/ jsxs(Space, {
            children: [
                /*#__PURE__*/ jsx(Cascader, {
                    "data-testid": "select-filter-field",
                    style: {
                        width: '160px'
                    },
                    fieldNames: fieldNames,
                    changeOnSelect: false,
                    value: dataIndex,
                    options: compile(fields),
                    onChange: onChange,
                    placeholder: t('Select field')
                }),
                /*#__PURE__*/ jsx(Select, {
                    "data-testid": "select-filter-operator",
                    style: {
                        minWidth: '110px'
                    },
                    popupMatchSelectWidth: false,
                    value: null == operator ? void 0 : operator.value,
                    options: compile(operators),
                    onChange: onOperatorsChange,
                    placeholder: t('Comparision')
                }),
                (null == operator ? void 0 : operator.noValue) ? null : /*#__PURE__*/ jsx(DynamicComponent, {
                    value: value,
                    schema: schema,
                    collectionField: collectionField,
                    onChange: setValue
                }),
                !props.disabled && /*#__PURE__*/ jsx("a", {
                    role: "button",
                    "aria-label": "icon-close",
                    children: /*#__PURE__*/ jsx(CloseCircleOutlined, {
                        onClick: remove,
                        style: removeStyle
                    })
                })
            ]
        })
    });
}, {
    displayName: 'FilterItem'
});
export { FilterItem };
