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__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__core_index_mjs__ from "../../core/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__ from "../../hooks/useProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_index_mjs__ from "../action/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__popover_index_mjs__ from "../popover/index.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      display: flex;\n      justify-content: flex-end;\n      width: 100%;\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      border-color: var(--colorSettings);\n      color: var(--colorSettings);\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const FilterActionContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
FilterActionContext.displayName = 'FilterActionContext';
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        container: css(_templateObject()),
        button: css(_templateObject1())
    };
});
const FilterAction = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { styles } = useStyles();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { designable, dn } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>props.form || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)(), []);
    const { options, onSubmit, onReset, ...others } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__.useProps)(props);
    const onOpenChange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((visible)=>{
        setVisible(visible);
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FilterActionContext.Provider, {
        value: {
            field,
            fieldSchema,
            designable,
            dn
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__popover_index_mjs__.StablePopover, {
            destroyTooltipOnHide: true,
            placement: 'bottomLeft',
            open: visible,
            onOpenChange: onOpenChange,
            trigger: 'click',
            content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("form", {
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.FormProvider, {
                    form: form,
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponent, {
                            schema: {
                                type: 'object',
                                properties: {
                                    filter: {
                                        type: 'string',
                                        enum: options || field.dataSource,
                                        default: fieldSchema.default,
                                        'x-component': 'Filter',
                                        'x-component-props': {}
                                    }
                                }
                            }
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            className: styles.container,
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SaveConditions, {}),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                        onClick: async ()=>{
                                            await form.reset();
                                            null == onReset || onReset(form.values);
                                            field.title = t('Filter');
                                            setVisible(false);
                                        },
                                        children: t('Reset')
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                        type: 'primary',
                                        htmlType: 'submit',
                                        onClick: (e)=>{
                                            e.preventDefault();
                                            e.stopPropagation();
                                            null == onSubmit || onSubmit(form.values);
                                            setVisible(false);
                                        },
                                        children: t('Submit')
                                    })
                                ]
                            })
                        })
                    ]
                })
            }),
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__action_index_mjs__.Action, {
                ...others,
                title: field.title
            })
        })
    });
}), {
    displayName: 'FilterAction'
});
const SaveConditions = ()=>{
    const { fieldSchema, field, designable, dn } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(FilterActionContext);
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { styles } = useStyles();
    if (!designable) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
        type: 'dashed',
        className: styles.button,
        onClick: ()=>{
            const defaultValue = {
                ...form.values.filter
            };
            fieldSchema.default = defaultValue;
            dn.emit('patch', {
                schema: {
                    'x-uid': fieldSchema['x-uid'],
                    default: undefinedToNull(defaultValue)
                }
            });
            dn.refresh();
        },
        children: t('Save conditions')
    });
};
function undefinedToNull(value) {
    const flat = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.flatten)(value);
    Object.keys(flat).forEach((key)=>{
        if (void 0 === flat[key]) flat[key] = null;
    });
    return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.unflatten)(flat);
}
export { FilterAction, FilterActionContext };
