"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    FilterActionContext: ()=>FilterActionContext,
    FilterAction: ()=>FilterAction
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_react_i18next_namespaceObject = require("react-i18next");
const withDynamicSchemaProps_js_namespaceObject = require("../../../application/hoc/withDynamicSchemaProps.js");
const index_js_namespaceObject = require("../../core/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const useProps_js_namespaceObject = require("../../hooks/useProps.js");
const external_action_index_js_namespaceObject = require("../action/index.js");
const external_popover_index_js_namespaceObject = require("../popover/index.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      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, external_react_namespaceObject.createContext)(null);
FilterActionContext.displayName = 'FilterActionContext';
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        container: css(_templateObject()),
        button: css(_templateObject1())
    };
});
const FilterAction = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((0, schema_namespaceObject.observer)((props)=>{
    const { styles } = useStyles();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.useField)();
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const { designable, dn } = (0, external_hooks_index_js_namespaceObject.useDesignable)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>props.form || (0, schema_namespaceObject.createForm)(), []);
    const { options, onSubmit, onReset, ...others } = (0, useProps_js_namespaceObject.useProps)(props);
    const onOpenChange = (0, external_react_namespaceObject.useCallback)((visible)=>{
        setVisible(visible);
    }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FilterActionContext.Provider, {
        value: {
            field,
            fieldSchema,
            designable,
            dn
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_popover_index_js_namespaceObject.StablePopover, {
            destroyTooltipOnHide: true,
            placement: 'bottomLeft',
            open: visible,
            onOpenChange: onOpenChange,
            trigger: 'click',
            content: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("form", {
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_js_namespaceObject.FormProvider, {
                    form: form,
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaComponent, {
                            schema: {
                                type: 'object',
                                properties: {
                                    filter: {
                                        type: 'string',
                                        enum: options || field.dataSource,
                                        default: fieldSchema.default,
                                        'x-component': 'Filter',
                                        'x-component-props': {}
                                    }
                                }
                            }
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                            className: styles.container,
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                                children: [
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SaveConditions, {}),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                        onClick: async ()=>{
                                            await form.reset();
                                            null == onReset || onReset(form.values);
                                            field.title = t('Filter');
                                            setVisible(false);
                                        },
                                        children: t('Reset')
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                        type: 'primary',
                                        htmlType: 'submit',
                                        onClick: (e)=>{
                                            e.preventDefault();
                                            e.stopPropagation();
                                            null == onSubmit || onSubmit(form.values);
                                            setVisible(false);
                                        },
                                        children: t('Submit')
                                    })
                                ]
                            })
                        })
                    ]
                })
            }),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_index_js_namespaceObject.Action, {
                ...others,
                title: field.title
            })
        })
    });
}), {
    displayName: 'FilterAction'
});
const SaveConditions = ()=>{
    const { fieldSchema, field, designable, dn } = (0, external_react_namespaceObject.useContext)(FilterActionContext);
    const form = (0, schema_namespaceObject.useForm)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { styles } = useStyles();
    if (!designable) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, client_namespaceObject.flatten)(value);
    Object.keys(flat).forEach((key)=>{
        if (void 0 === flat[key]) flat[key] = null;
    });
    return (0, client_namespaceObject.unflatten)(flat);
}
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
});
