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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE__common_useNiceDropdownHeight_mjs__ from "../../../common/useNiceDropdownHeight.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__ from "../../../flag-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_style_mjs__ from "../components/style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_index_mjs__ from "../context/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      .ant-popover-inner {\n        padding: ",
        ";\n        .ant-menu-submenu-title {\n          margin-block: 0;\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const defaultWrap = (s)=>s;
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css, token } = param;
    return {
        popover: css(_templateObject(), "".concat(token.paddingXXS, "px 0"))
    };
});
function withInitializer(C) {
    const WithInitializer = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
        const { designable, insertAdjacent } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const { isInSubTable } = (0, __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.useFlag)() || {};
        const { insert, useInsert, wrap = defaultWrap, insertPosition = 'beforeEnd', onSuccess, designable: propsDesignable, popoverProps, children, popover = true, style, componentProps } = props;
        const insertCallback = useInsert ? useInsert() : insert;
        const insertSchema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((schema)=>{
            if (insertCallback) insertCallback(wrap(schema, {
                isInSubTable
            }));
            else insertAdjacent(insertPosition, wrap(schema, {
                isInSubTable
            }), {
                onSuccess
            });
        }, [
            insertCallback,
            wrap,
            insertAdjacent,
            insertPosition,
            onSuccess
        ]);
        const { wrapSSR, hashId, componentCls } = (0, __WEBPACK_EXTERNAL_MODULE__components_style_mjs__.useSchemaInitializerStyles)();
        const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
        const { token } = __WEBPACK_EXTERNAL_MODULE_antd__.theme.useToken();
        const { styles } = useStyles();
        const dropdownMaxHeight = (0, __WEBPACK_EXTERNAL_MODULE__common_useNiceDropdownHeight_mjs__.useNiceDropdownMaxHeight)([
            visible
        ]);
        const cProps = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
                options: props,
                style,
                ...componentProps
            }), [
            componentProps,
            props,
            style
        ]);
        if (!designable && true !== propsDesignable) return null;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__context_index_mjs__.SchemaInitializerContext.Provider, {
            value: {
                visible,
                setVisible,
                insert: insertSchema,
                options: props
            },
            children: false === popover ? /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(C, cProps) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Popover, {
                placement: 'bottomLeft',
                ...popoverProps,
                arrow: false,
                overlayClassName: styles.popover,
                open: visible,
                onOpenChange: setVisible,
                content: wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: "".concat(componentCls, " ").concat(hashId),
                    style: {
                        maxHeight: dropdownMaxHeight,
                        overflowY: 'auto'
                    },
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider, {
                        theme: {
                            components: {
                                Menu: {
                                    itemHeight: token.marginXL,
                                    borderRadius: token.borderRadiusSM,
                                    itemBorderRadius: token.borderRadiusSM,
                                    subMenuItemBorderRadius: token.borderRadiusSM
                                }
                            }
                        },
                        children: children
                    })
                })),
                children: /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(C, cProps)
            })
        });
    }, {
        displayName: "WithInitializer(".concat(C.displayName || C.name, ")")
    });
    return WithInitializer;
}
export { withInitializer };
